Further optimize: simplify heuristics calculation and cache component values
Co-authored-by: THoguet <61246905+THoguet@users.noreply.github.com>
This commit is contained in:
1 parent
601e7855aa
commit
4e7eb18bc5
2 files changed
+13
-23
No files matched your search
@@ -16,6 +16,10 @@ import { CalendarSettingsService } from '../../services/calendar-settings-servic
|
|||||||
export class CalendarYear {
|
export class CalendarYear {
|
||||||
protected isLoading = signal(true);
|
protected isLoading = signal(true);
|
||||||
|
|
||||||
|
// Cache current month and year to avoid repeated Date object creation
|
||||||
|
private readonly currentMonth = new Date().getMonth();
|
||||||
|
private readonly currentYear = new Date().getFullYear();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private userInput: UserInputService,
|
private userInput: UserInputService,
|
||||||
private calendarService: CalendarService,
|
private calendarService: CalendarService,
|
||||||
@@ -45,13 +49,10 @@ export class CalendarYear {
|
|||||||
protected selectedDates: SelectedDates = new SelectedDates();
|
protected selectedDates: SelectedDates = new SelectedDates();
|
||||||
|
|
||||||
getYear(monthIndex: number): number {
|
getYear(monthIndex: number): number {
|
||||||
const offset = new Date().getMonth() <= monthIndex ? 0 : 1;
|
const offset = this.currentMonth <= monthIndex ? 0 : 1;
|
||||||
return new Date().getFullYear() + offset;
|
return this.currentYear + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
// months starting from actual date
|
// months starting from actual date - cache this array
|
||||||
protected months: number[] = Array.from(
|
protected months: number[] = Array.from({ length: 12 }, (_, i) => (this.currentMonth + i) % 12);
|
||||||
{ length: 12 },
|
|
||||||
(_, i) => (new Date().getMonth() + i) % 12,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
@@ -469,27 +469,16 @@ export class SelectedDates implements SelectedDateInterface {
|
|||||||
return aStart - bStart;
|
return aStart - bStart;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const oneDayInMs = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
// PRIMARY: Optimize adjacency - reduce "little holes" between vacation periods
|
// PRIMARY: Optimize adjacency - reduce "little holes" between vacation periods
|
||||||
for (let i = 0; i < sorted.length - 1; i++) {
|
for (let i = 0; i < sorted.length - 1; i++) {
|
||||||
const current = sorted[i];
|
const current = sorted[i];
|
||||||
const next = sorted[i + 1];
|
const next = sorted[i + 1];
|
||||||
|
|
||||||
// Get dates as UTC strings to remove timezone issues
|
// Use timestamps directly - dates should already be normalized
|
||||||
const currentEndDate = new Date(current.range.end ?? new Date());
|
const currentEndTime = current.range.end?.getTime() ?? 0;
|
||||||
const nextStartDate = new Date(next.range.start ?? new Date());
|
const nextStartTime = next.range.start?.getTime() ?? 0;
|
||||||
|
|
||||||
// Convert to date-only strings (YYYY-MM-DD) to compare calendar days
|
|
||||||
const currentEndString = currentEndDate.toISOString().split('T')[0];
|
|
||||||
const nextStartString = nextStartDate.toISOString().split('T')[0];
|
|
||||||
|
|
||||||
// Parse back to dates at UTC midnight
|
|
||||||
const currentEndDateNormalized = new Date(currentEndString + 'T00:00:00Z');
|
|
||||||
const nextStartDateNormalized = new Date(nextStartString + 'T00:00:00Z');
|
|
||||||
|
|
||||||
const currentEndTime = currentEndDateNormalized.getTime();
|
|
||||||
const nextStartTime = nextStartDateNormalized.getTime();
|
|
||||||
|
|
||||||
const oneDayInMs = 24 * 60 * 60 * 1000;
|
|
||||||
|
|
||||||
// Check if adjacent (next day) - this is the best case!
|
// Check if adjacent (next day) - this is the best case!
|
||||||
if (nextStartTime - currentEndTime === oneDayInMs) {
|
if (nextStartTime - currentEndTime === oneDayInMs) {
|
||||||
|
|||||||
Reference in new issue
Block a user