From f7a418f10d19051ed2a7dd69d808100e9a8edcf4 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:16:54 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20realtime=20metrics=20tot?= =?UTF-8?q?al=20calculation=20to=20eliminate=20redundant=20loops?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- src/pages/realtime-metrics.tsx | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/pages/realtime-metrics.tsx b/src/pages/realtime-metrics.tsx index 8e0834a..77b8363 100644 --- a/src/pages/realtime-metrics.tsx +++ b/src/pages/realtime-metrics.tsx @@ -290,21 +290,24 @@ export const Component = () => { .map(([category]) => category); }, [categoryTotals]); - const hasTotal = useMemo( - () => filteredChartData.some((point) => point.isTotal), - [filteredChartData], - ); - - const totalRequests = useMemo(() => { - if (!filteredChartData.length) return 0; - if (hasTotal) { - return filteredChartData.reduce( - (sum, point) => (point.isTotal ? sum + point.value : sum), - 0, - ); + const { hasTotal, totalRequests } = useMemo(() => { + let has = false; + let totalSum = 0; + let nonTotalSum = 0; + for (let i = 0; i < filteredChartData.length; i++) { + const point = filteredChartData[i]; + if (point.isTotal) { + has = true; + totalSum += point.value; + } else { + nonTotalSum += point.value; + } } - return filteredChartData.reduce((sum, point) => sum + point.value, 0); - }, [filteredChartData, hasTotal]); + return { + hasTotal: has, + totalRequests: has ? totalSum : nonTotalSum, + }; + }, [filteredChartData]); const topCategories = useMemo(() => { return sortedCategories