Tutorial
Your first benchmark
Write a suite that compares two ways to sum numbers, run it with measure, and read the table it prints.
In this tutorial you write a benchmark that compares two ways to add up
100 000 numbers, run it, and read what measure prints. It takes about ten
minutes, and you need Node.js 24 or later.
1. Install measure and the duration measure
In a new directory:
npm init --yesnpm pkg set type=modulenpm install --save-dev @ac-bench/cli @ac-bench/measure-duration
@ac-bench/cli is the measure command. @ac-bench/measure-duration is
the measure that times code; measures are plugins, so you install the ones
you use.
2. Write the suite
Create src/sum.bench.ts:
import { durationCase, durationCondition } from "@ac-bench/measure-duration";
const values = Array.from({ length: 100_000 }, (_, index) => index * 0.5);
durationCondition("sum 100 000 numbers", () => {
durationCase("for loop", () => {
let total = 0;
for (let index = 0; index < values.length; index++) total += values[index]!;
return total;
});
durationCase("reduce", () => values.reduce((total, value) => total + value, 0));
});A condition groups cases that are alternatives to each other; a case is one implementation. The input is built outside the cases, because everything inside a case is timed. Each case returns its result, so the engine cannot skip work whose result nobody reads.
3. Write the config
Create bench.config.ts beside package.json:
import { defineConfig } from "@ac-bench/cli";
import durationPlugin from "@ac-bench/measure-duration/plugin";
export default defineConfig({
plugins: [durationPlugin],
});measure finds every *.bench.ts file by default. Check what it found,
without measuring:
npx measure -c bench.config.ts --listsum 100 000 numbers (Duration) for loop reduce
4. Run it
npx measure -c bench.config.tssum 100 000 numbers CASE MEDIAN (MS) CI (MS) MAD (MS) P95 (MS) RATE (OP/S) N REL MARGIN★ for loop 0.039 ±0.002 ±0.000 0.042 25,247 18,990 1.00× ⚠ see notes reduce 0.482 ±0.003 ±0.016 0.628 1,980 1,507 12.48× ±0.42× ⚠ see notes
The run takes a few seconds: each case runs in fresh Node processes, three of them here, and their results are pooled. Your numbers will differ; the shape of the table will not.
5. Read the table
| column | what it says |
|---|---|
MEDIAN (MS) |
the typical time of one call, in milliseconds |
CI (MS) |
how far the median could be off: its confidence interval |
MAD (MS) |
how spread the samples are around the median |
P95 (MS) |
95 % of the calls were at least this fast |
RATE (OP/S) |
calls per second, from the median |
N |
how many samples were taken |
REL, MARGIN |
each case against the fastest one, marked ★, with the margin of that ratio |
Here the for loop is about 12 times faster than reduce, give or take
0.42×. ⚠ see notes marks a case measure has a warning about, printed above
the table: outliers, a first sample much slower than the rest, or other
processes busy on the machine while it measured.
What you have done
You wrote a suite, configured measure and read its table. Next,
Your first gate turns the comparison into a check that can
fail a build.