Benchmark test
A benchmark test measures how fast code runs — or how much memory it uses — instead of checking that an answer is right. The result is a number, which you compare with earlier numbers or a budget, so a change that makes things slower is noticed before users notice it.
What makes a good benchmark test
- Measures, not asserts — it reports a number, such as milliseconds per operation, instead of passing or failing on a right answer.
- Repeated — it runs the code many times and reports an average or median, so random noise does not mislead you.
- Comparable — it runs under the same conditions every time, so today’s number can be compared with last week’s.
- Budgeted — agree how much slower is too slow, and fail the build when a change goes past it.
The shape of a benchmark test
A benchmark names the work to time, and the tool does the repeating and reporting.
import { bench } from 'vitest';
bench('sort 10,000 numbers', () => {
sort(numbers);
});
Benchmarks can run in continuous integration too, so a slowdown is caught when it is introduced.