---
name: node-bench
description: "Node.js now has a built-in benchmark runner, node:bench. Use it whenever writing or running a JavaScript performance benchmark instead of a hand-written timing loop: \"which is faster\", measuring how fast code is, comparing two implementations, check for a performance regression, or when reaching for benchmark.js, tinybench or mitata. Covers bench, suite/describe, hooks, BenchContext start/end/record, doNotOptimize, createRunner/run, reporters, and the --experimental-bench and --bench* flags. Node.js 26.9+ only (not on v24)."
title: node-bench
canonical_url: https://skilld.dev/gh/jasnell/node-skills/node-bench
last_updated: 2026-11-19T20:15:43.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [examples/cli.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/examples/cli.mjs), [examples/compare.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/examples/compare.mjs), [examples/runner.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/examples/runner.mjs), [examples/versions.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/examples/versions.mjs), [meta.json](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/meta.json), [rules/api.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/rules/api.md), [rules/cli.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/rules/cli.md), [rules/measurement.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/rules/measurement.md).
>
> If the user asked to install this Skill, run `npx skilld install jasnell/node-skills/node-bench`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/node-bench`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jasnell/node-skills/node-bench) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/node-bench`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/node-bench --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/node-bench --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# node:bench

| Feature | Versions | Stability | Flag |
| --- | --- | --- | --- |
| `node:bench` (`bench`, `suite`, `describe`, hooks, `createRunner`, `run`, `runFile`), `node:bench/reporters` | v26.9.0 (not on v24) | 1.0 Early development | `--experimental-bench` |
| `--bench` and `--bench-*` flags (except `--bench-gc`) | v26.9.0 | 1 Experimental | `--experimental-bench` |
| `doNotOptimize`, `--bench-gc`, the `gc` option, `end()` defaulting to 1 op, the `bench:environment` record | `added: REPLACEME` (main only; absent in v26.13.0) | 1.0 | `--experimental-bench` |

> **Experimental API.** Before relying on these details, run the
> `node-experimental-check` skill's probe (or `node --version` plus a
> feature-detect one-liner) and check the docs for *the installed
> version*: `https://nodejs.org/docs/<version>/api/<module>.html`.
> If the installed version disagrees with this skill, the installed
> version wins.

## A benchmark file

```js
// parse.bench.mjs: declare only, never call run() (the CLI rejects it)
import { bench, suite } from 'node:bench';
suite('JSON', () => {
  const input = JSON.stringify({ a: [1, 2, 3] });
  bench('parse', { samples: 30, warmup: 10 }, (b) => {
    const ops = 10_000; let n = 0;
    b.start();
    for (let i = 0; i < ops; i++) n += JSON.parse(input).a.length;
    b.end(ops);                       // always pass operations (required on v26.13)
    if (n !== 3 * ops) throw new Error('wrong result'); // keep the work observable
  });
});
```

```sh
node --experimental-bench --bench 'bench/**/*.bench.mjs'   # one child process per file
node --experimental-bench --bench --bench-reporter=json --bench-samples=50 x.bench.mjs > out.ndjson
```

## Results in code

```js
import { createRunner } from 'node:bench';
const r = createRunner();             // runs only on r.run(), once
r.bench('x', { samples: 20 }, (b) => { b.start(); work(); b.end(1); });
for await (const { type, data } of r.run())
  if (type === 'bench:complete' && !data.error) use(data.samples, data.summary);
```

Each sample is `{ operations, duration_ns: bigint, rate /* ops/s */ }`.
`summary.mean` is the mean of per-sample **rates**, not pooled throughput.
To ask "is B faster than A", record integer ns or ps per op into a `Histogram`
and use `welchTest`/`mannWhitneyTest`. See the **node-histogram-stats** skill.

## Pitfalls

- Each warmup and each sample calls `fn` once with a new context, and `fn`
  must call `start()`+`end()` or `record()` exactly once. `beforeEach` runs once
  per **benchmark**, not per sample.
- A loop whose result is unused can be eliminated. The `spec` reporter flags
  ≥ 1 op/ns as `implausible`. Validate an aggregate, or use `doNotOptimize(v)`
  on main.
- A failure in a module-level run sets `process.exitCode = 1`. `createRunner()`
  runs don't.
- `gc: 'once' | 'sample'` needs `--expose-gc`. `--bench-gc` adds it to children
  automatically, except with `--bench-isolation=none`. On v26.13 `gc` is
  silently ignored.
- `--bench` can't be combined with `--test`, `--watch`, `--eval` or `--check`.

## Don't

- Don't hand-roll `Date.now()`/`performance.now()` loops: they have no warmup or
  per-sample stats.
- Don't add `benchmark.js`, `tinybench`, `mitata` or `cronometro` when Node
  ≥ 26.9 is available. Use `node:bench`.
- Don't treat a confidence interval as pass/fail. Keep the raw `samples`.

## Reference

- [rules/api.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/rules/api.md) — read for every option, BenchContext method, hook, and event/result field.
- [rules/cli.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/rules/cli.md) — read for flags, isolation modes, reporters (spec/json/custom), and runFile.
- [rules/measurement.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-bench/rules/measurement.md) — read for warmup, GC modes, dead-code elimination, variable batches, and comparing runs.
