Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Why is a single manual Date.now()-based timing measurement of a JavaScript function considered unreliable?
💻 Code Challenge | +75 XP
Write a tinybench comparison between two implementations of a data-transformation function (a for-loop version and an Array.reduce version) against the same realistic dataset, and interpret the resulting ops/sec table.
A pull request claims a new implementation is "much faster" based on a single manual Date.now() timing, but the reviewer is skeptical. Reorder the steps to get a rigorous, trustworthy comparison.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Drawing a performance conclusion from a single manual Date.now() before/after measurement
// Wrong: unreliable, one-shot measurement
const start = Date.now();
myFunction();
console.log(Date.now() - start);
// Correct: statistically reliable, accounts for JIT warm-up
const bench = new Bench();
bench.add("myFunction", () => myFunction());
await bench.run();The Solution //
A single execution is heavily influenced by V8's JIT compiler still warming up, and by system noise (background processes, garbage collection timing) — it doesn't represent steady-state performance. Use a proper microbenchmarking library like tinybench, which runs many iterations and accounts for warm-up.
The Error //
Comparing two implementations against different input data or under different conditions
// Wrong: not a fair, isolated comparison
bench.add("A", () => implA(dataset1));
bench.add("B", () => implB(dataset2)); // different data — invalid
// Correct: only the implementation differs
bench.add("A", () => implA(sharedDataset));
bench.add("B", () => implB(sharedDataset));The Solution //
A valid benchmark comparison must change exactly one variable — the implementation itself. Testing implementation A against a small dataset and implementation B against a large one (or on different hardware, with other processes running) produces a comparison where the measured difference could be explained by the uncontrolled variable rather than the actual code being compared.