Node.js Micro-benchmarking Suite

0.14.0 · active · verified Sun Apr 19

Bench-node is a powerful Node.js module specifically designed for micro-benchmarking JavaScript code blocks, accurately measuring operations per second (ops/sec). Its current stable version is 0.14.0, and it maintains an active development pace with frequent minor releases every few weeks to months, indicating robust maintenance. A primary differentiator of `bench-node` is its explicit use of V8 deoptimization (via `%NeverOptimizeFunction`) to ensure that benchmarked code is not aggressively optimized away by the V8 engine. While this approach yields highly stable and reproducible results for micro-benchmarks, users should be aware that these "accurate" measurements might not perfectly reflect "realistic" performance in a fully optimized production environment. To further assist developers, the library includes an opt-in Dead Code Elimination (DCE) detection plugin that warns if benchmarked code is being optimized out, although enabling it disables V8 deoptimization. Additionally, `bench-node` offers statistical significance testing (Welch's t-test) to evaluate if observed performance differences are statistically meaningful, a crucial feature for benchmarking in high-variance environments. It provides a rich set of built-in reporters (text, chart, HTML, JSON, CSV, pretty) for result visualization and ships with TypeScript types for enhanced developer experience. Other features include support for setup/teardown routines, execution in worker threads for isolation, and both operations and time-based benchmarking modes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up a `Suite` with a custom reporter, add multiple benchmarks, and run them. It compares different array population methods and explicitly uses the result to prevent dead code elimination.

import { Suite, chartReport } from 'bench-node';

const suite = new Suite({
  // Optionally configure the reporter
  reporter: chartReport,
  reporterOptions: {
    printHeader: true // Controls whether system info header is printed
  }
});

suite.add('Using Array.push', () => {
  const arr = [];
  for (let i = 0; i < 1000; i++) {
    arr.push(i);
  }
  return arr.length; // Ensure result is used to prevent DCE
});

suite.add('Using Array literal concatenation', () => {
  let arr = [];
  for (let i = 0; i < 1000; i++) {
    arr = [...arr, i];
  }
  return arr.length; // Ensure result is used to prevent DCE
});

suite.add('Using direct index assignment', () => {
  const arr = [];
  for (let i = 0; i < 1000; i++) {
    arr[i] = i;
  }
  return arr.length; // Ensure result is used to prevent DCE
});

suite.run();

view raw JSON →