Small but mighty benchmarking library for JavaScript
MIT License
Benchmate is a small but mighty benchmarking library for JavaScript.
process.hrtime
and performance.now
.setup
and teardown
(before and after the entire test, not each execution).async
support, but you really shouldn't benchmark async functions.Benchmate is published on the NPM registry: https://npmjs.com/package/benchmate
npm install -D benchmate
pnpm install -D benchmate
yarn add -D benchmate
Notes:
min
, max
, and percentiles will me meaningless, as you get a single measurement.import { Bench } from "benchmate";
// The default options are sensible,
const bench = new Bench({
iterations: "auto", // number of iterations, must be "auto" when using time
time: 1000, // target running time for tasks
batching: { // batching improves accuracy by a <lot>
enabled: true,
size: "auto" // number of iterations per batch or "auto"
},
warmup: {
enabled: true,
size: "auto", // number of warmup iterations or "auto" for (iterations / 10)
},
method: "auto", // "auto" | "hrtime" | "performance.now" - measurement method, defaults to best available
testSleepDuration: 0, // how long to sleep between tasks (ms)
quiet: false, // don't print anything, defaults to `true` in browsers, `false` in Node
setup: () => Promise<void> | void, // function to run before each test
teardown: () => Promise<void> | void, // function to run after each test
});
bench.add("RegExp#test", () => {
if (!/o/.test("Hello World!")) console.log("nop");
});
bench.add("String#indexOf", () => {
if ("Hello World!".indexOf("o") === -1) console.log("nop");
});
await bench.run();
// ^ returns an array of benchmark results:
// type BenchmarkResult = {
// name: string;
// stats: {
// samples: number;
// batches: number;
// time: { // all timings are in milliseconds
// total: number;
// min: number;
// max: number;
// average: number;
// percentile50: number;
// percentile90: number;
// percentile95: number;
// };
// opsPerSecond: {
// average: number;
// max: number;
// min: number;
// margin: number; // percentage
// };