diff --git a/.jules/bolt.md b/.jules/bolt.md index 9345ee7..08c67ca 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -1,4 +1,8 @@ ## 2024-05-18 - Avoid array spreads inside loops for Map grouping **Learning:** In Next.js/React applications, when grouping items (like schedules or talks) into a `Map` where the values are arrays, using the array spread operator `[...existing, item]` inside a loop (like `forEach` or `map`) causes amortized O(N^2) memory allocations and unnecessary Garbage Collection overhead. -**Action:** Always use `.push()` on the existing array reference if the data structure permits local mutation. For strict ESLint configurations enforcing `no-restricted-syntax`, extract the existing array, push to it, and handle the fallback elegantly (`if (!existing) { map.set(key, [item]); } else { existing.push(item); }`). + +## 2024-05-25 - Avoid array includes inside array filter operations + +**Learning:** In Next.js/React applications, when filtering a list of items based on their presence in another array (e.g., `savedSessionIds.includes(s.id)` inside a `filter` function), it leads to an O(N \* M) time complexity. This can cause performance bottlenecks and block the main thread, especially if the arrays are large. +**Action:** Convert the lookup array into a `Set` before the filtering loop. Use `set.has(id)` inside the `filter` function to achieve O(N + M) time complexity. diff --git a/components/schedule/ScheduleContainer.tsx b/components/schedule/ScheduleContainer.tsx index 33d877a..2817940 100644 --- a/components/schedule/ScheduleContainer.tsx +++ b/components/schedule/ScheduleContainer.tsx @@ -20,7 +20,9 @@ export default function ScheduleContainer({ initialSchedule, year }: Readonly sessions.filter((s) => savedSessionIds.includes(s.id) || s.isServiceSession); + const savedIdsSet = new Set(savedSessionIds); + + const filterSessions = (sessions: GridSession[]) => sessions.filter((s) => savedIdsSet.has(s.id) || s.isServiceSession); return initialSchedule.map((day) => ({ ...day,