From 46e1351aedf16749d34fb4eaf7aa046452f2a21f Mon Sep 17 00:00:00 2001 From: Yan <75355375+yancat160@users.noreply.github.com> Date: Fri, 8 May 2026 09:17:07 -0400 Subject: [PATCH] cache hex6 hash per label in color mapping Address Greptile review on PR #4257: hex6(key) was parsed twice per label, once in the sort comparator and once when deriving the preferred palette slot. Cache the parsed integer in a local hashOf map so the sort comparator and the slot lookup share one computation per label, no behaviour change. --- src/components/metrics/EventsChart.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/metrics/EventsChart.tsx b/src/components/metrics/EventsChart.tsx index cbd868e34..473b8d20c 100644 --- a/src/components/metrics/EventsChart.tsx +++ b/src/components/metrics/EventsChart.tsx @@ -61,11 +61,12 @@ export function EventsChart({ websiteId, focusLabel, limit }: EventsChartProps) // (the FNV prime is close to 2^24). const colorByKey: Record = {}; const used = new Set(); - const orderedKeys = [...Object.keys(map)].sort( - (a, b) => parseInt(hex6(a), 16) - parseInt(hex6(b), 16), + const hashOf = Object.fromEntries( + Object.keys(map).map(key => [key, parseInt(hex6(key), 16)]), ); + const orderedKeys = [...Object.keys(map)].sort((a, b) => hashOf[a] - hashOf[b]); for (const key of orderedKeys) { - const start = (parseInt(hex6(key), 16) >>> 4) % CHART_COLORS.length; + const start = (hashOf[key] >>> 4) % CHART_COLORS.length; let chosen = CHART_COLORS[start]; for (let i = 0; i < CHART_COLORS.length; i++) { const candidate = CHART_COLORS[(start + i) % CHART_COLORS.length];