From 3cd1868f8a5e4d81897c453c8cc0cd01846db9f8 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 24 Apr 2026 01:11:25 +0000 Subject: [PATCH] perf: extract inline array allocation in Heatmap component Co-authored-by: artosien <65523959+artosien@users.noreply.github.com> --- benchmark.js | 29 ++++++++++++++++++++++++++++ src/components/analytics/heatmap.tsx | 4 +++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 benchmark.js diff --git a/benchmark.js b/benchmark.js new file mode 100644 index 0000000..46b5507 --- /dev/null +++ b/benchmark.js @@ -0,0 +1,29 @@ +const ITERATIONS = 10_000_000; + +function bench(name, fn) { + const start = performance.now(); + let dummy = 0; + for (let i = 0; i < ITERATIONS; i++) { + dummy += fn()[0]; // Use the array to prevent dead-code elimination + } + const end = performance.now(); + console.log(`${name}: ${end - start}ms`); + return dummy; +} + +// Inside inline allocation +function runInline() { + return [0, 0, 0, 0, 0, 0]; +} + +// Outside allocation +const EMPTY_SET = [0, 0, 0, 0, 0, 0]; +function runConstant() { + return EMPTY_SET; +} + +console.log(`Running benchmark with ${ITERATIONS.toLocaleString()} iterations...`); + +// We need to run them without interfering JIT behavior. +bench("Constant Array Reference", runConstant); +bench("Inline Array Allocation", runInline); diff --git a/src/components/analytics/heatmap.tsx b/src/components/analytics/heatmap.tsx index 531133c..62ef2b9 100644 --- a/src/components/analytics/heatmap.tsx +++ b/src/components/analytics/heatmap.tsx @@ -56,6 +56,8 @@ const LOTTO_GAMES = [ { id: "658", name: "Ultra Lotto 6/58", max: 58 }, ] +const EMPTY_SET = [0, 0, 0, 0, 0, 0] + export function LotteryHeatmap() { const { toast } = useToast() const [lottoType, setLottoType] = React.useState("658") @@ -402,7 +404,7 @@ export function LotteryHeatmap() {
- {(generatedSet.length > 0 ? generatedSet : [0, 0, 0, 0, 0, 0]).map((num, i) => { + {(generatedSet.length > 0 ? generatedSet : EMPTY_SET).map((num, i) => { const numData = numbers.find(n => n.num === num) const freq = numData?.frequency || 0 return (