Explanation
How a case is sampled
A duration case is sampled until its estimate is precise enough, within a minimum and a maximum count and time. Why the loop has that shape.
A duration case is sampled until its estimate is precise enough, bounded by a minimum and a maximum count and time. This page explains why the loop is shaped as it is.
A sample is a span, not a call
The clock steps in quanta: 27 ns in Node, 5 to 20 µs in a cross-origin isolated
browser page. A sample shorter than a hundred quanta is dominated by rounding,
so duration calibrates a span: it calls the case k times per sample
(callsPerSample: "auto"), doubling k until one span covers
minSpanQuanta × resolution (100 steps by default), and reports the time per
operation (operationsPerCall divides further). That is also what makes a
2 ns operation measurable on any runtime: the span is long, the division is
exact.
Each call’s return value is consumed — numbers summed, anything else kept — so an engine cannot remove a body whose result nobody reads.
Warm-up is measured in time
warmupMs runs the case before sampling until that much time has passed (and
at least warmup calls). JIT tiers are reached by time spent, not by a fixed
count, and a fixed count is either too few for a slow case or wasteful for a
fast one.
Stopping on precision
Sampling stops once the confidence interval of the median, relative to the
median, is narrower than relativeError, and at least minRuns samples and
minTimeMs have been spent. relativeError: "auto" derives the target from the
spread this machine shows for this case, so a noisy CI runner and a quiet
workstation each get a target they can reach. maxRuns and maxTimeMs cap a
case that never settles; it is then reported with the precision it reached and
a warning. "off" reproduces a fixed-count loop, which a differential run
wants.
The median, not the mean: benchmark distributions are right-skewed (a garbage collection or a preemption adds time, nothing subtracts it), and the median’s interval does not widen for one late sample.
Defaults at three levels
Sampling options resolve from the measure’s defaults, then the run’s
(sampling in the config, per
measure id: { duration: { minRuns: 20 } }), then each condition’s own
sampling option.
A gate never needs a tighter case. It reads every execution’s interval and, when it cannot decide, asks for more executions rather than more samples: see How a gate decides.
Warnings rather than silence
What the loop cannot fix, it reports:
| warning | what it means |
|---|---|
outliers |
a heavy tail: a few samples far slower than the rest |
bimodal-samples |
two speeds: two code paths, or a layout lottery inside the process |
overhead-dominates |
the harness’s own cost is a large part of each span |
too-few-samples |
the caps stopped the loop before the target precision |
The table marks the case, and the archive keeps the warning beside its numbers.