Installation
First install the plugin@codspeed/tinybench-plugin
and tinybench
(if not already installed):
The CodSpeed plugin now requires
tinybench
v4 and above. Note that tinybench v4 has dropped support for CommonJS modules, so ESM is now required.Usage
Creating benchmarks
Let’s create a fibonacci function and benchmark it with tinybench and the CodSpeed plugin:benches/bench.ts
- We create a simple recursive fibonacci function.
-
We create a new
Bench
instance with CodSpeed support by using thewithCodSpeed
helper. This step is critical to enable CodSpeed on your benchmarks. -
We add two benchmarks to the suite and launch it, benching our
fibonacci
function for 10 and 15.
Testing the benchmarks locally
Now, we can run our benchmarks locally to make sure everything is working as expected:TypeScript runnerTo run the
.ts
file directly, we recommend using
esbuild-register
. It allows
running TypeScript & ESM files directly with Node.js.Integrating into a bigger project, multiple benchmark files
Often time you will not be writing your benchmarks in a single file. Indeed, it can become quite difficult to maintain a single file with all your benchmarks as your project grows.You can find the source code for the following example in the
examples of the
codspeed-node
repository.
There are multiple examples available, for CJS, ESM, JavaScript, and TypeScript.file-structure
- The
src
directory contains the source code of the project. Here we have two files,fibonacci.ts
andfoobarbaz.ts
. - The
bench
directory contains the benchmarks for the project. There is a file for each source file that defines benchmarks for it. - The
bench/index.bench.ts
file is the entry point for the benchmarks. It imports all the other benchmark files and runs them.
bench/fibo.bench.ts
Bench
as a parameter and
then adds some benchmarks to it. This will allow us to add benchmarks to the
same suite from multiple files.
bench/index.bench.ts
Check out the full for this example:
with-typescript-cjs
in the codspeed-node
repository.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.If you want more details on how to configure the CodSpeed action, you can check
out the Continuous Reporting section.
main
branch and every
pull request:
.github/workflows/codspeed.yml
Limitations
Async code execution profiling
Execution profiles for async code can sometimes be unreliable, as profiling tools may lose stack trace information due to the event loop.If your code is fully sync, consider using tinybench’s
runSync
as an entrypoint to improve accuracy.