From 9708aa9ed38d59abe7f1194c0bbe394e5e07b45f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 23 Apr 2026 21:38:51 +0000 Subject: [PATCH] perf: Optimize maxFreq calculation in MiniHeatmap Replaced `Math.max(...numbers.map(n => n.frequency))` with `numbers.reduce((max, n) => Math.max(max, n.frequency), 0)` to calculate the maximum frequency. This prevents the redundant creation of an intermediate array using `map` before passing the values to `Math.max`. By using `reduce`, we iterate over the numbers array once, updating the running maximum, which is more memory and CPU efficient, especially as the size of the array scales. It also inherently handles the edge case of an empty array by passing `0` as the initial accumulator, cleanly replacing the ternary check. Co-authored-by: artosien <65523959+artosien@users.noreply.github.com> --- src/components/analytics/mini-heatmap.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/analytics/mini-heatmap.tsx b/src/components/analytics/mini-heatmap.tsx index 4a8405d..3731860 100644 --- a/src/components/analytics/mini-heatmap.tsx +++ b/src/components/analytics/mini-heatmap.tsx @@ -21,7 +21,7 @@ export function MiniHeatmap() { setNumbers(generated) }, []) - const maxFreq = numbers.length > 0 ? Math.max(...numbers.map(n => n.frequency)) : 0 + const maxFreq = numbers.reduce((max, n) => Math.max(max, n.frequency), 0) const getColor = (freq: number) => { if (maxFreq === 0) return "bg-primary/10"