From 7b1a2d6c63052f58f5262e036022f3057e3a4004 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 05:17:19 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20fallback=20calculation?= =?UTF-8?q?=20loop=20using=20a=20single=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced two maps (denominators, fallbackDenominators) with a single Map tracking an object per timestamp. Reduces iteration overhead. Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- bun.lock | 1 - src/pages/realtime-metrics.tsx | 35 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/bun.lock b/bun.lock index c812ee9..26496de 100644 --- a/bun.lock +++ b/bun.lock @@ -1,6 +1,5 @@ { "lockfileVersion": 1, - "configVersion": 1, "workspaces": { "": { "name": "pushy-admin", diff --git a/src/pages/realtime-metrics.tsx b/src/pages/realtime-metrics.tsx index 8e0834a..caf7c31 100644 --- a/src/pages/realtime-metrics.tsx +++ b/src/pages/realtime-metrics.tsx @@ -232,26 +232,22 @@ export const Component = () => { const selectedPoints = chartData.filter( (point) => point.isTotal || point.attribute === selectedAttribute, ); - const denominators = new Map(); - const fallbackDenominators = new Map(); + const timeTotals = new Map< + string, + { total: number; fallback: number; hasTotal: boolean } + >(); for (const point of selectedPoints) { + let entry = timeTotals.get(point.time); + if (!entry) { + entry = { total: 0, fallback: 0, hasTotal: false }; + timeTotals.set(point.time, entry); + } if (point.isTotal) { - denominators.set( - point.time, - (denominators.get(point.time) || 0) + point.value, - ); + entry.total += point.value; + entry.hasTotal = true; } else { - fallbackDenominators.set( - point.time, - (fallbackDenominators.get(point.time) || 0) + point.value, - ); - } - } - - for (const [time, value] of fallbackDenominators) { - if (!denominators.has(time)) { - denominators.set(time, value); + entry.fallback += point.value; } } @@ -259,7 +255,12 @@ export const Component = () => { if (point.isTotal) { return point; } - const denominator = denominators.get(point.time) || 0; + const entry = timeTotals.get(point.time); + const denominator = entry + ? entry.hasTotal + ? entry.total + : entry.fallback + : 0; if (denominator <= 0) { return point; }