@jonahsnider/benchmark
A Node.js benchmarking library with support for multithreading and TurboFan optimization isolation.
Install
This library is published on the npm registry and can be installed with your package manager of choice:
yarn add @jonahsnider/benchmark
npm i @jonahsnider/benchmark
Your project must use ESModules in order to import the library.
Usage
Below are some common use cases for the library.
You can also read the full documentation or the API overview for more details on the package API.
Basic usage
This is a basic example that doesn't take advantage of multithreading (which is fine for simple benchmarks!).
You can view the example here or by following the guide below:
import {Benchmark, Suite} from '@jonahsnider/benchmark';
const benchmark = new Benchmark();
const concatenation = new Suite('concatenation', {
run: {
durationMs: 3000,
},
warmup: {
trials: 1_000_000,
},
});
concatenation
.addTest('+', () => 'a' + 'b')
.addTest('templates', () => `${'a'}${'b'}`)
.addTest('.concat()', () => 'a'.concat('b'));
const results = await benchmark.runSuites();
console.log(results);
const suiteResults = await concatenation.run();
console.log(suiteResults);
Multithreading
Enabling multithreading allows you to run each suite in parallel which results in much faster benchmarks and keeps TurboFan optimizations isolated to each suite.
This is useful for functions that are prone to deoptimizations when a different code path is triggered from a different suite.
You can view an example of this here or by following the guide below:
First, we create our suites in individual files.
This is required since each file will be loaded in a separate thread.
In a new directory called ./suites/
create a file called substring.js
:
import {Suite} from '@jonahsnider/benchmark';
const suite = new Suite('substring', {
filepath: import.meta.url,
run: {
trials: 1000,
},
warmup: {
durationMs: 10_000,
},
});
suite.addTest('substring', () => {
const string = 'abcdef';
return string.substring(1, 4);
});
suite.addTest('substr', () => {
const string = 'abcdef';
return string.substr(1, 4);
});
suite.addTest('slice', () => {
const string = 'abcdef';
return string.slice(1, 4);
});
export default suite;
If you want to create other suites you can do that now.
Once every suite is created we need to create the main file in ./index.js
:
import {Benchmark} from '@jonahsnider/benchmark';
import substringSuite from './suites/substring.js';
const benchmark = new Benchmark();
await benchmark.addSuite(substringSuite, {threaded: true});
const results = await benchmark.runSuites();
console.log(results);
Advanced usage
This library is designed to be very modular and customizable by allowing you to use your own structures instead of the built-in ones.
When registering a suite with Benchmark#addSuite
you can pass an instance of a Suite
or you could pass your own structure that the implements the SuiteLike
interface.
You can also use Suite
s directly without using Benchmark
at all or make a tool as an alternative for Benchmark
.
Refer to the API overview and documentation for more information.
Terminology
A Benchmark
is the top-level object that helps manage your suites.
You can use multiple benchmarks if you'd like but usually you only need one.
A Suite
is a group of tests that are run as a group.
Each test in a suite should be a different implementation for the same thing.
For example, you could make a suite for "sorting an array" and your tests could be "insertion sort", "selection sort", and "bubble sort".
A test is a single function that is run many times while its performance is recorded.
Internally it's represented with the Test
class which is exported by the package but you probably don't need to use it directly.