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() {