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 {
|
||||
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(
|
||||
private userInput: UserInputService,
|
||||
private calendarService: CalendarService,
|
||||
@@ -45,13 +49,10 @@ export class CalendarYear {
|
||||
protected selectedDates: SelectedDates = new SelectedDates();
|
||||
|
||||
getYear(monthIndex: number): number {
|
||||
const offset = new Date().getMonth() <= monthIndex ? 0 : 1;
|
||||
return new Date().getFullYear() + offset;
|
||||
const offset = this.currentMonth <= monthIndex ? 0 : 1;
|
||||
return this.currentYear + offset;
|
||||
}
|
||||
|
||||
// months starting from actual date
|
||||
protected months: number[] = Array.from(
|
||||
{ length: 12 },
|
||||
(_, i) => (new Date().getMonth() + i) % 12,
|
||||
);
|
||||
// months starting from actual date - cache this array
|
||||
protected months: number[] = Array.from({ length: 12 }, (_, i) => (this.currentMonth + i) % 12);
|
||||
}
|
||||
@@ -469,27 +469,16 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
return aStart - bStart;
|
||||
});
|
||||
|
||||
const oneDayInMs = 24 * 60 * 60 * 1000;
|
||||
|
||||
// PRIMARY: Optimize adjacency - reduce "little holes" between vacation periods
|
||||
for (let i = 0; i < sorted.length - 1; i++) {
|
||||
const current = sorted[i];
|
||||
const next = sorted[i + 1];
|
||||
|
||||
// Get dates as UTC strings to remove timezone issues
|
||||
const currentEndDate = new Date(current.range.end ?? new Date());
|
||||
const nextStartDate = new Date(next.range.start ?? new Date());
|
||||
|
||||
// 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;
|
||||
// Use timestamps directly - dates should already be normalized
|
||||
const currentEndTime = current.range.end?.getTime() ?? 0;
|
||||
const nextStartTime = next.range.start?.getTime() ?? 0;
|
||||
|
||||
// Check if adjacent (next day) - this is the best case!
|
||||
if (nextStartTime - currentEndTime === oneDayInMs) {
|
||||
|
||||
Reference in new issue
Block a user