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
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

**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-23 β€” O(1) Lookups inside filter loops

**Learning:** `Array.prototype.includes` within `Array.prototype.filter` or `.map` can create O(N*M) bottlenecks if the lookup array is sizable, negatively affecting JS execution time (especially on critical render paths for large lists like schedules).
**Action:** Always convert lookup arrays to `Set`s before array iteration and use `.has()` for O(1) checks. Ensure to place the `Set` initialization *outside\* any loops. Also, be sure to clean up `.orig` files created by patch tools before committing.
3 changes: 2 additions & 1 deletion components/schedule/ScheduleContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default function ScheduleContainer({ initialSchedule, year }: Readonly<Sc
return initialSchedule;
}

const filterSessions = (sessions: GridSession[]) => sessions.filter((s) => savedSessionIds.includes(s.id) || s.isServiceSession);
const savedSessionIdsSet = new Set(savedSessionIds);
const filterSessions = (sessions: GridSession[]) => sessions.filter((s) => savedSessionIdsSet.has(s.id) || s.isServiceSession);
Comment on lines +23 to +24
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 the current optimization to O(N+M) is excellent, you can further improve React performance by ensuring referential equality for rooms and sessions that are not affected by the filter. This prevents unnecessary re-renders of child components like SessionCard or ScheduleGrid sub-sections.

Consider checking if the filtered array length matches the original length to return the original reference.

    const savedSessionIdsSet = new Set(savedSessionIds);
    const filterSessions = (sessions: GridSession[]) => {
      const filtered = sessions.filter((s) => savedSessionIdsSet.has(s.id) || s.isServiceSession);
      return filtered.length === sessions.length ? sessions : filtered;
    };


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