From 5589f36604f1713a799f34c52deabe40a3f6b4bd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 21:13:40 +0000 Subject: [PATCH] Lifted invariant `Math.max(values.length - 1, 1)` calculation outside of `.map()` loops in chart components to prevent redundant CPU overhead on every iteration during rendering. Co-authored-by: Mihir-Rabari <169349389+Mihir-Rabari@users.noreply.github.com> --- .Jules/bolt.md | 4 ++++ apps/console/src/app/(console)/dashboard/page.tsx | 4 +++- apps/console/src/components/console/charts.tsx | 8 ++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.Jules/bolt.md b/.Jules/bolt.md index 2f6ee69e..530299c5 100644 --- a/.Jules/bolt.md +++ b/.Jules/bolt.md @@ -1,3 +1,7 @@ ## 2026-11-01 - Replace Math.max with reduce **Learning:** Using `Math.max(...array)` with the spread operator on potentially large arrays creates an O(N) intermediate memory allocation and risks 'Maximum call stack size exceeded' errors. **Action:** Use a memory-efficient `Array.prototype.reduce()` pass for array maximum/minimum calculations instead. + +## 2026-11-08 - Lift loop invariants in map +**Learning:** Identifying invariant calculations (e.g., `Math.max(values.length - 1, 1)`) inside `.map()` iterations or React render loops and lifting them outside the loop into a constant prevents redundant CPU overhead on every iteration. +**Action:** Lift invariant loop calculations into pre-computed constants whenever possible to reduce redundant execution overhead. diff --git a/apps/console/src/app/(console)/dashboard/page.tsx b/apps/console/src/app/(console)/dashboard/page.tsx index 54cb1ed9..976149d6 100644 --- a/apps/console/src/app/(console)/dashboard/page.tsx +++ b/apps/console/src/app/(console)/dashboard/page.tsx @@ -32,9 +32,11 @@ function TrendChart({ // ⚡ Bolt: Avoid O(N) intermediate array allocation and spread operator overhead by using a single reduce pass. const max = values.reduce((m, v) => (v > m ? v : m), 1); + // ⚡ Bolt: Lift invariant calculation outside of map loop + const xDenominator = Math.max(values.length - 1, 1); const linePoints = values .map((value, index) => { - const x = (index / Math.max(values.length - 1, 1)) * 100; + const x = (index / xDenominator) * 100; const y = 92 - (value / max) * 70; return `${x},${y}`; }) diff --git a/apps/console/src/components/console/charts.tsx b/apps/console/src/components/console/charts.tsx index e62449c7..b2dd58d7 100644 --- a/apps/console/src/components/console/charts.tsx +++ b/apps/console/src/components/console/charts.tsx @@ -44,9 +44,11 @@ export function LinePulse({ // ⚡ Bolt: Avoid O(N) intermediate array allocation and spread operator overhead by using a single reduce pass. const max = values.reduce((m, v) => (v > m ? v : m), 1); + // ⚡ Bolt: Lift invariant calculation outside of map loop + const xDenominator = Math.max(values.length - 1, 1); const points = values .map((value, index) => { - const x = (index / Math.max(values.length - 1, 1)) * 100; + const x = (index / xDenominator) * 100; const y = 100 - (value / max) * 84; return `${x},${y}`; }) @@ -95,9 +97,11 @@ export function TrendAreaChart({ // ⚡ Bolt: Avoid O(N) intermediate array allocation and spread operator overhead by using a single reduce pass. const max = values.reduce((m, v) => (v > m ? v : m), 1); + // ⚡ Bolt: Lift invariant calculation outside of map loop + const xDenominator = Math.max(values.length - 1, 1); const linePoints = values .map((value, index) => { - const x = (index / Math.max(values.length - 1, 1)) * 100; + const x = (index / xDenominator) * 100; const y = 92 - (value / max) * 70; return `${x},${y}`; })