Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/schedule/ScheduleContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export default function ScheduleContainer({ initialSchedule, year }: Readonly<Sc
return initialSchedule;
}

const filterSessions = (sessions: GridSession[]) => sessions.filter((s) => savedSessionIds.includes(s.id) || s.isServiceSession);
// Convert savedSessionIds to a Set to improve lookup performance from O(N) to O(1)
const savedIdsSet = new Set(savedSessionIds);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While converting the array to a Set here successfully improves the filtering performance from O(N*M) to O(N+M), this conversion still occurs every time the useMemo for filteredSchedule is re-evaluated (e.g., when initialSchedule changes or when showSavedOnly is toggled).

For a more robust architectural improvement, consider storing savedSessionIds as a Set directly within the ScheduleContext. This would eliminate the need for local conversion and also optimize the isSaved check in the context, which currently uses Array.prototype.includes() (O(M) complexity). This would provide O(1) lookups throughout the application without repeated overhead.

const filterSessions = (sessions: GridSession[]) => sessions.filter((s) => savedIdsSet.has(s.id) || s.isServiceSession);

return initialSchedule.map((day) => ({
...day,
Expand Down
Loading