Performance regression tests
Prerequisite: node-capacity-and-saturation. Objective: Define reproducible workload and baseline. Tested locally on Node.js 22.16.0 Linux. Each example is independent. Use a fresh disposable working directory, save as main.mjs, then run node main.mjs. The harness uses a five-second timeout and synthetic environment. These are static code blocks, not ByteMind interactive execution.
How it works
Keep input size, runtime, warmup, sample count, concurrency, and environment fixed. Record raw samples and summarize with a median or appropriate percentiles; do not hide outliers. Compare a baseline and a candidate one change at a time. Use an absolute budget and a relative change threshold chosen from repeatability measurements, not arbitrary numbers on shared CI. Timing is noisy; use repeated runs and investigate reproducible failures. The fixture below validates actual timings and results without declaring a winner. Its second example is a synthetic release-policy decision, not a measured benchmark.
Measure two equivalent lookup strategies
import {performance} from 'node:perf_hooks';import assert from 'node:assert/strict';const ids=Array.from({length:1000},(_,i)=>i), set=new Set(ids);const queries=[0,500,999,-1];for(const lookup of [x=>ids.includes(x),x=>set.has(x)]){ const run=()=>{let hits=0;for(let i=0;i<1000;i++)for(const q of queries)hits+=Number(lookup(q));return hits;}; for(let i=0;i<3;i++)assert.equal(run(),3000); const samples=[];for(let i=0;i<5;i++){const t=performance.now();assert.equal(run(),3000);samples.push(performance.now()-t);} assert.ok(samples.every(x=>Number.isFinite(x)&&x>=0)); console.log('5 samples; 3000 hits');}Expected output:
5 samples; 3000 hits5 samples; 3000 hitsSet creation is outside this lookup-only workload. Include construction cost separately for a fair end-to-end comparison.
Apply an illustrative median budget
function median(a){const s=[...a].sort((a,b)=>a-b);return s[Math.floor(s.length/2)];}const base=median([10,11,12]),candidate=median([14,15,16]);console.log(candidate>base*1.2?'investigate':'within policy');Expected output:
investigateThe synthetic medians are 11 and 15; a 20% policy flags the change. This is not a universal threshold.
Common mistakes
Never claim a speedup merely because one measurement was lower; measure construction, memory and errors when they affect the workload.
Practice
Design a benchmark for a cache hit and miss distribution; specify warmups, input, correctness assertions and error budget.
Reference candidate: node-performance-regression-tests-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/perf_hooks.html (opened).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance regression tests — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Why check equal results before comparing duration?
Which comparison is appropriate for a lookup-only benchmark?
A single slow CI run crosses a noisy threshold. What next?