Skip to content

Getting trustworthy numbers

Suppose two implementations are within 3% on your laptop. Before choosing one, check what each invocation does, how the harness samples it, and whether the difference repeats on the runtime you ship.

For a sorting benchmark, “sort this mutable list” and “return a sorted copy” have different costs. For the second question, clone the same unsorted input inside each timed variant and return the sorted copy. For a read-only operation, build the fixture once outside the timer. Use setup and teardown for resources that should live across all the invocations in a measurement.

Run with --verify to compare the variants’ first results. Agreement catches mistakes such as joining strings with different separators. A known-answer unit test can also catch a bug shared by both variants. Try more than one size or input shape if the deployed workload includes both. Designing comparisons

  1. Warmup invokes the operation before measurement until its per-call time stops improving, and at least for the preset’s warmup. Measuring before the JIT settles is the main reason two runs of the same benchmark disagree, so the warmup can run past the preset on a cold process. The observed cost also estimates how many calls fit in a batch.
  2. Batch timing puts many fast calls under one timer. Elapsed batch time divided by its invocation count gives one per-run sample. A blocking pause during a batch affects that sample.
  3. Alternating rounds distribute a column’s measurement budget across its variants. With two variants, one round visits A then B, the next B then A. This reduces sensitivity to gradual drift during a run.
  4. Summary reports the 10th percentile of the batch samples as runtimeMicros. Interference and scheduler preemption only ever make a batch slower, so the low end is the cost of the work itself and repeats from run to run, while the median moves with the machine’s load. In a Measurement, samples counts measured batches and rounds counts measured invocations. The separate option BenchmarkOptions.rounds controls the alternation schedule.
Preset Warmup (at least) Exercise budget (at most) Batch target Alternating rounds
--quick 20 ms 300 ms 5 ms 4
Default 100 ms 2 s 25 ms 4
--thorough 500 ms 5 s 25 ms 8

The last two columns are timing targets. The exercise budget is an upper bound: measuring stops as soon as the low end has settled (40 batches by default), so a run usually ends well before it. A slow operation can exceed the budget in a single batch. Start with a quick run to check the fixture, then use a longer preset to investigate a close result. The benchmark class example shows the lower-level benchmark API.

The runner defaults to a native AOT executable. Use --target=jit for code deployed on the Dart VM with JIT compilation. For a web workload, --target=js runs compiled JavaScript on Node and --target=wasm runs the Wasm build on Node 22 or later. If browser APIs or rendering are part of the workload, measure in that browser as well.

Use the same SDK, machine, power settings, runtime, and input for before/after runs. Schedule benchmark processes sequentially and let background builds finish. The report records the runtime and UTC time, plus the Dart version when available. Record the machine and workload with the results.

Read the representative time together with its spread. relativeDeviation is the median absolute deviation divided by the representative time: how far a typical batch ran above the floor. cv is the sample standard deviation divided by the mean and responds more strongly to occasional slow batches. Neither is a confidence interval.

The first measurement in a process runs on a colder JIT than later ones, so comparing two benchmark programs means comparing their first measurement, or running each in its own process.

If a small lead changes direction across repeated runs, collect more evidence before choosing an implementation. If it persists, consider its absolute effect: saving one microsecond in a function called ten times per request saves about ten microseconds per request. Use a threshold that reflects the cost your project cares about when checking regressions.

All guides · Reports and regression checks