Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
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);
const savedIdsSet = new Set(savedSessionIds);

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

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