Installation
First install the plugin@codspeed/vitest-plugin
and vitest (if not already installed):
The CodSpeed plugin is compatible with
vitest
v3.2 and above. Vitest 5
requires
@codspeed/vitest-plugin>=6.0.0.
This page uses the Vitest 5 syntax. For the Vitest 3 and 4 syntax, see the
@codspeed/vitest-plugin README.Usage
Creating benchmarks
Let’s create a fibonacci function and benchmark it with Vitest’sbench fixture
and the CodSpeed plugin:
- TypeScript
- JavaScript
src/fibo.bench.ts
- We create a simple recursive fibonacci function.
- We create a
vitesttest"fibo"that receives thebenchfixture. - We declare two benchmarks with
bench()and run each with.run(), benching ourfibonaccifunction for 10 and 15. CodSpeed reports eachbench()as its own benchmark, e.g.,src/fibo.bench.ts::fibo::fibo 10.
bench.compare instead of calling .run() on each one:
bench() as its own benchmark, e.g.,
src/compare.bench.ts::fibo 15::recursive.
Create or update the vitest.config.ts file to use the CodSpeed runner:
vitest.config.ts
Testing the benchmarks locally
Now, we can run our benchmarks locally to make sure everything is working as expected:--reporter=verbose to display the results of each benchmark (for example, with
pnpm):
terminal
Troubleshooting errorsIf you encounter an error like
Failed to resolve "@codspeed/vitest-plugin" from "vitest.config.ts", you might
need to rename the vitest.config.ts file to vitest.config.mts.You can find more information in the
Vite documentation on importing ESM package.vitest.
You can now
run those benchmarks in your CI to get
consistent performance measurements.
Running the benchmarks in your CI
To generate performance reports, you need to run the benchmarks in your CI. This allows CodSpeed to automatically run benchmarks and warn you about regressions during development. Here is an example of a GitHub Actions workflow that runs the benchmarks and reports the results to CodSpeed on every push to themain branch and every
pull request:
.github/workflows/codspeed.yml
Recipes
Running benchmarks in parallel CI jobs
To parallelize your benchmarks, you can use theshard options from vitest.
For example with pnpm on Github Actions:
Same benchmark with different variationsFor now, you cannot run the same benchmarks several times within the same run.
If the same benchmark is run multiple times, you will receive the following
comment on your pull request:

To combine measurement modes like simulation and memory, check out the
documentation on running multiple instruments
serially.