> ## Documentation Index
> Fetch the complete documentation index at: https://codspeed.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# codspeed-node documentation

> Options and environment variables of the CodSpeed Node.js integrations

Each `@codspeed/*` package wraps a benchmarking library. This page lists the
options that CodSpeed adds. For the options of the libraries themselves, see
their documentation.

## Vitest

```ts theme={null}
codspeedPlugin(options?: { forks?: number }): Plugin
```

`codspeedPlugin` takes the options of
[`@codspeed/vitest-plugin`](https://codspeed.io/docs/benchmarks/nodejs/vitest). Configure the benchmarks
with the [Vitest benchmarking options](https://vitest.dev/guide/benchmarking).

**forks** `number` (default: 3)

Number of [forks](#process-isolation) per walltime benchmark. Requires Vitest
5\.

```ts vitest.config.ts theme={null}
import codspeedPlugin from "@codspeed/vitest-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
  plugins: [codspeedPlugin({ forks: 5 })],
});
```

CodSpeed also adds `forks` to the options of a single benchmark and to the
options of `bench.compare`, which apply to every benchmark of the comparison:

```ts src/fibo.bench.ts theme={null}
import { test } from "vitest";

function fibonacci(n: number): number {
  if (n < 2) {
    return n;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}

test("fibo", async ({ bench }) => {
  await bench("fibo 10", { forks: 5 }, () => {
    fibonacci(10);
  }).run();

  await bench.compare(
    bench("fibo 15", () => {
      fibonacci(15);
    }),
    bench("fibo 20", () => {
      fibonacci(20);
    }),
    { forks: 2 }
  );
});
```

The `forks` option of a benchmark takes precedence over the one of
`bench.compare`, which takes precedence over the plugin option.

## tinybench

```ts theme={null}
withCodSpeed(bench: Bench, options?: { forks?: number }): Bench
```

`withCodSpeed` takes the options of
[`@codspeed/tinybench-plugin`](https://codspeed.io/docs/benchmarks/nodejs/tinybench) as its second
argument. Configure the `Bench` itself and its tasks with the
[tinybench options](https://tinylibs.github.io/tinybench/).

**forks** `number` (default: 3)

Number of [forks](#process-isolation) per walltime benchmark.

```ts benches/bench.ts theme={null}
const bench = withCodSpeed(new Bench(), { forks: 5 });
```

## benchmark.js

```ts theme={null}
withCodSpeed(suite: Benchmark.Suite): Benchmark.Suite
withCodSpeed(benchmark: Benchmark): Benchmark
```

[`@codspeed/benchmark.js-plugin`](https://codspeed.io/docs/benchmarks/nodejs/benchmarkjs) adds no
options. Configure the suite and its benchmarks with the
[benchmark.js options](https://benchmarkjs.com/docs). The integration measures
all benchmarks in a single process and doesn't support
[process isolation](#process-isolation).

## Playwright

The options of [`@codspeed/playwright-plugin`](https://codspeed.io/docs/benchmarks/nodejs/playwright)
are listed in its [API section](https://codspeed.io/docs/benchmarks/nodejs/playwright.md#api). The flow
itself uses the [Playwright API](https://playwright.dev/docs/api/class-page).
The integration doesn't support [process isolation](#process-isolation).

## Environment variables

Environment variables take precedence over the options set in code, e.g., to
change the number of forks in CI without changing code. Set them in the `env` of
the step that runs your benchmarks.

**CODSPEED\_WALLTIME\_FORKS** `number`

Number of [forks](#process-isolation) per walltime benchmark, for tinybench
and Vitest. Overrides the `forks` options. Must be a positive integer.

**CODSPEED\_PROCESS\_ISOLATION** `boolean` (default: true)

Set to `false` or `0` to measure all benchmarks in a single process. The
`forks` options and `CODSPEED_WALLTIME_FORKS` are then ignored. Other values
than `true`, `1`, `false`, and `0` fail the run.

**CODSPEED\_PLAYWRIGHT\_ROUNDS** `number`

Number of measurement rounds of each Playwright benchmark. Overrides the
`rounds` option. Must be a positive integer.

## Process isolation

With the [walltime instrument](https://codspeed.io/docs/instruments/walltime.md), the tinybench and Vitest
integrations measure every benchmark in several fresh Node.js processes, called
forks, and combine their rounds into a single result. To learn why this makes a
difference, see [Process Isolation](https://codspeed.io/docs/features/process-isolation.md).

Forks run one after the other, and each fork runs the full measurement budget
set by `time` and `iterations`. With the default of 3 forks, a walltime run
takes about 3 times longer than a run in a single process. Process isolation is
on by default, does not apply to the other instruments, and requires
`@codspeed/*` packages `>=7.0.0-beta.0`.

> [!NOTE]
>
> With Vitest, process isolation requires Vitest 5. With Vitest 3 and 4, all
> benchmarks run in a single process and the `forks` options are ignored.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.