[5.0] `bench.compare` no longer shows relative scores
Describe the bug
In Vitest v4, the benchmark results showed a little compare list after everything was done, with relative labels like "1.5x". In Vitest v5, the benchmark tables themselves only seem to show "fastest" and "slowest".
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-hsm7fwu6?file=test%2Fbasic.bench.ts
import { test } from 'vitest';
test('foo bar', async ({ bench }) => {
await bench.compare(
bench('foo', () => JSON.stringify(123)),
bench('bar', () => JSON.stringify({ foo: 'bar', baz: 123 }))
);
});
System Info
System:
OS: Linux 6.18 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
CPU: (24) x64 AMD Ryzen 9 7950X 16-Core Processor
Memory: 18.22 GB / 23.47 GB
Container: Yes
Shell: 3.6.1 - /usr/bin/fish
Binaries:
Node: 24.15.0 - /mnt/wslg/runtime-dir/fnm_multishells/70914_1788521686026/bin/node
npm: 11.12.1 - /mnt/wslg/runtime-dir/fnm_multishells/70914_1788521686026/bin/npm
pnpm: 11.10.0 - /mnt/wslg/runtime-dir/fnm_multishells/70914_1788521686026/bin/pnpm
Browsers:
Chrome: 151.0.7922.71
npmPackages:
vitest: catalog: => 5.0.0
Used Package Manager
pnpm
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
I have thought of some ux choices for how to present the relative score, so let me share. I'm not sure always treating 1.00x as fastest is necessary the most intuitive one as implemented by https://github.com/vitest-dev/vitest/pull/11203.
Vitest 4
-- RUN --
...test tree..
name hz ...
JSON.stringify 9,300,000 ... fastest
library serializer 6,900,000 ...
custom serializer 5,800,000 ... slowest
-- BENCH SUMMARY --
Fastest: JSON.stringify
1.35x faster than library serializer
1.61x faster than custom serializer
1. Normalized relative speed
name hz ... relative speed
JSON.stringify 9,300,000 ... [1.00x] fastest
library serializer 6,900,000 ... [0.74x]
custom serializer 5,800,000 ... [0.62x] slowest
2. Normalized percentage
name hz ... relative speed
JSON.stringify 9,300,000 ... 100% fastest
library serializer 6,900,000 ... 74%
custom serializer 5,800,000 ... 62% slowest
Or the variant of this is to show -xx% to emphasize the difference:
name hz ...
JSON.stringify 9,300,000 ... fastest
library serializer 6,900,000 ... -26% slower
custom serializer 5,800,000 ... -38% slower
3. Relative time
name hz ... relative time
JSON.stringify 9,300,000 ... 1.00x fastest
library serializer 6,900,000 ... 1.35x
custom serializer 5,800,000 ... 1.61x slowest
4. Explicit slowdown in the table
name hz
JSON.stringify 9,300,000 ... fastest
library serializer 6,900,000 ... 1.35x slower
custom serializer 5,800,000 ... 1.61x slower
5. Per-table prose summary
name hz ...
JSON.stringify 9,300,000 ... fastest
library serializer 6,900,000 ...
custom serializer 5,800,000 ... slowest
Fastest: JSON.stringify
1.35x faster than library serializer
1.61x faster than custom serializer
This preserves the explanatory part of the v4 UX while keeping it attached to the bench.compare() call that produced the table. It costs additional vertical space for every comparison group.
Additionally, we should likely provide a way to pick one specific bench in bench.compare as a baseline.
await bench.compare(
bench('x', () => JSON.stringify(123)),
bench('y', { baseline: true }, () => JSON.stringify({ foo: 'bar', baz: 123 }))
bench('z', () => JSON.stringify({ foo: 'bar', baz: 123, bar: 456 }))
);
name ... comparison
x ... +14% faster
y ... (baseline)
z ... -20% slower
We decided on
- Add
baseline: booleanoption tobench(name, { baseline: true }, ...)to specify baseline of comparison. - If baseline isn't specified, take the fastest one as basline.
- for display, it would show like:
with baseline
await bench.compare(
bench('x', () => JSON.stringify(123)),
bench('y', { baseline: true }, () => JSON.stringify({ foo: 'bar', baz: 123 }))
bench('z', () => JSON.stringify({ foo: 'bar', baz: 123, bar: 456 }))
);
name ... comparison
x ... +14% faster
y ... (baseline)
z ... -20% slower
without baseline
name ... comparison
x ... (baseline) // fastest picked as baseline
y ... -14% slower
z ... -34% slower
Could I take this, following the design above? Unless someone on the team already plans to.
One question before I start: bench.from(name, source) has no options argument, but a stored result from bench.from() is the most natural baseline, as in the docs example bench.compare(bench('current', ...), bench.from('baseline', './my-bench.json')). Should bench.from() also accept { baseline: true }? Or should a bench.from() entry become the baseline automatically when nothing else is marked?
Small follow-up so the open question doesn't block this. My plan is to add { baseline: true } to bench() exactly as described above, and leave bench.from() without options for now. If nothing is marked, the fastest entry is the baseline, bench.from() entries included. bench.from() can get a baseline option later if it turns out to be needed.
Unless someone on the team is already working on it, I'll start with that and post a branch here before opening a PR.