Check out the documentation for complete integration instructions.
First, install the plugin @codspeed/vitest-plugin and vitest (if not already installed):
Note
The CodSpeed plugin is only compatible with vitest@3.2 and above.
npm install --save-dev @codspeed/vitest-plugin vitestor with yarn:
yarn add --dev @codspeed/vitest-plugin vitestor with pnpm:
pnpm add --save-dev @codspeed/vitest-plugin vitestLet's create a fibonacci function and benchmark it with Vitest's bench fixture:
import { describe, test } from "vitest";
function fibonacci(n: number): number {
if (n < 2) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
describe("fibonacci", () => {
test("depth", async ({ bench }) => {
await bench.compare(
bench("fibonacci10", () => {
fibonacci(10);
}),
bench("fibonacci15", () => {
fibonacci(15);
}),
);
});
});Note
The bench fixture is a Vitest 5 API. On Vitest 3 and 4, benchmarks are
declared with the top-level bench() export instead:
import { bench, describe } from "vitest";
describe("fibonacci", () => {
bench("fibonacci10", () => {
fibonacci(10);
});
});Create or update your vitest.config.ts file to use the CodSpeed runner:
import { defineConfig } from "vitest/config";
import codspeedPlugin from "@codspeed/vitest-plugin";
export default defineConfig({
plugins: [codspeedPlugin()],
// ...
});Finally, run your benchmarks (here with pnpm):
$ pnpm vitest bench --run
... Regular `vitest bench` outputAnd... Congrats 🎉, CodSpeed is installed in your benchmarking suite! Locally, CodSpeed will fallback to vitest since the instrumentation is only available in the CI environment for now.
You can now run those benchmarks in your CI to continuously get consistent performance measurements.