Skip to content

Commit 5d3ddfb

Browse files
committed
bench: stabilize cached-source construction benchmark
Store the freshly constructed CachedSource instances into a pre-sized sink array so V8 cannot dead-code-eliminate them and memory pressure stays consistent between samples. Overwriting existing slots (rather than push + length reset) keeps the sink's hidden class stable and avoids resize allocations during measurement. This reduces run-to-run variance that was surfacing as false codspeed regressions on this task.
1 parent 7db8867 commit 5d3ddfb

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

benchmark/cases/cached-source/index.bench.mjs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import { createHash } from "crypto";
1010
import sources from "../../../lib/index.js";
1111
import { fixtureCode, fixtureMap, noop } from "../../fixtures.mjs";
1212

13+
/**
14+
* Pre-sized sink used by allocation-heavy tasks to retain the constructed
15+
* instances past the loop so V8 cannot dead-code-eliminate them and so
16+
* memory pressure is predictable between samples. Overwriting existing
17+
* slots (rather than push/length reset) keeps the sink's hidden class
18+
* stable and avoids resize allocations during measurement.
19+
*/
20+
const CONSTRUCT_BATCH = 100;
21+
const sink = Array.from({ length: CONSTRUCT_BATCH });
22+
1323
/**
1424
* A CachedSource with all the common caches already populated. Reused
1525
* across tasks that explicitly measure the warm path.
@@ -31,8 +41,8 @@ const warmed = (() => {
3141
*/
3242
export default function register(bench) {
3343
bench.add("cached-source: new CachedSource()", () => {
34-
for (let i = 0; i < 100; i++) {
35-
new sources.CachedSource(new sources.RawSource(fixtureCode));
44+
for (let i = 0; i < CONSTRUCT_BATCH; i++) {
45+
sink[i] = new sources.CachedSource(new sources.RawSource(fixtureCode));
3646
}
3747
});
3848

0 commit comments

Comments
 (0)