diff --git a/.jules/bolt.md b/.jules/bolt.md index 231af188b..ed489c9e6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -10,3 +10,7 @@ ## 2024-05-24 - Memoizing inline array maps **Learning:** Inline mapping of arrays inside JSX in large React components causes O(N) recalculation on every render. **Action:** Wrap inline JSX elements that map over arrays (e.g., lists of tasks) in a `useMemo` hook with specific dependencies. + +## 2024-05-24 - Memoizing calendar grid generation +**Learning:** Inline array generation and mapping within JSX (e.g., `Array.from({ length: 35 }).map(...)`) causes O(N) re-renders and blocks the main thread during unrelated state updates, especially for grid components like calendars. +**Action:** Wrap inline array generation and mapping inside JSX in a `useMemo` hook to avoid unnecessary re-renders. diff --git a/frontend/src/components/calendar/CalendarMonthView.tsx b/frontend/src/components/calendar/CalendarMonthView.tsx index ae8b731d6..e40b6c00c 100644 --- a/frontend/src/components/calendar/CalendarMonthView.tsx +++ b/frontend/src/components/calendar/CalendarMonthView.tsx @@ -20,6 +20,26 @@ export function CalendarMonthView({ visibleMonthEvents }: Props) { return grouped; }, [visibleMonthEvents]); + // ⚡ Bolt Performance Optimization: + // Wrapped the inline array mapping of 35 grid cells inside a `useMemo` hook. + // Impact: Eliminates O(N) element creation (35 iterations) and prevents blocking + // the main thread during unrelated state updates, significantly reducing render time. + const gridCells = useMemo(() => { + return Array.from({ length: 35 }).map((_, i) => { + const dayEvents = monthEventsByDay.get(i) ?? []; + return ( +
+ {i < 31 ? i + 1 : ''} + {dayEvents.map((event) => ( +
+ {event.time} {event.title} +
+ ))} +
+ ); + }); + }, [monthEventsByDay]); + return (
@@ -27,19 +47,7 @@ export function CalendarMonthView({ visibleMonthEvents }: Props) {
{/* Simulated Grid Cells */} - {Array.from({ length: 35 }).map((_, i) => { - const dayEvents = monthEventsByDay.get(i) ?? []; - return ( -
- {i < 31 ? i + 1 : ''} - {dayEvents.map((event) => ( -
- {event.time} {event.title} -
- ))} -
- ); - })} + {gridCells}
);