Skip to content
ethan-daviesPublic

About

A benchmarking framework for JavaScript and TypeScript that makes it easy to write, run, analyse, and compare performance benchmarks.

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Benched

A benchmarking framework for JavaScript and TypeScript that makes it easy to write, run, analyse, and compare performance benchmarks.

Developers write benchmark files using the Benched library, then execute them using the benched CLI. During execution, Benched displays a live terminal interface showing benchmark progress, statistics, and results. After completion, results can be compared, exported, or used in CI pipelines to detect performance regressions.

Installation

pnpm add -D benched-js

Quick Start

Create a benchmark file:

// array.bench.ts
import { suite, bench } from "benched-js";

suite("Array Operations", () => {
  bench("Array.push()", () => {
    const arr: number[] = [];
    for (let i = 0; i < 10000; i++) {
      arr.push(i);
    }
  });

  bench("Array spread", () => {
    let arr: number[] = [];
    for (let i = 0; i < 10000; i++) {
      arr = [...arr, i];
    }
  });
});

Run it:

benched run array.bench.ts

CLI

Run benchmarks

benched run [files...]
benched run **/*.bench.ts
benched run --tag parser
benched run array.bench.ts --export json
benched run array.bench.ts --export csv
benched run --workers 4 --no-ui
benched run --verbose
Flag Description
--tag <tag> Run only benchmarks with the given tag
--export <format> Export results as json or csv
--workers <n> Number of worker threads for parallel execution
--no-ui Disable live terminal UI (useful in CI)
--verbose Show min, max, and ops/s statistics

Compare results

benched compare baseline.json current.json

Example output:

JSON.parse()
  Before   1.82ms
  After    1.47ms
  19.2% faster

Configuration

Create a benched.config.ts file in your project root:

export default {
  workers: 4,
  parallel: true,
  samples: 50,
  warmup: 32,
  timeout: 5000,

  export: {
    formats: ["json"],
    includeEnvironment: true,
    includeSamples: false,
    outputDir: ".",
  },

  ui: {
    theme: "default",
    liveUpdates: true,
  },
};

See benched.config.example.ts for a full example.

API

suite(name, fn)

Define a group of related benchmarks.

bench(name, fn, options?)

Define a single benchmark.

bench("Hash", fn, {
  warmup: 500,
  samples: 100,
  iterations: 1,
  timeout: 5000,
  tags: ["parser", "json"],
});

Async benchmarks

bench("Database query", async () => {
  await db.query("SELECT 1");
});

Features

Statistical Analysis

Each benchmark reports:

  • Mean, median, min, max
  • Standard deviation and variance
  • Operations per second
  • Sample count

Warm-up Runs

Warm-up iterations run before sampling to reduce JIT compilation effects. Configure per benchmark or globally.

Parallel Execution

Run independent benchmarks concurrently using worker threads:

benched run --workers 4

Enable parallel: true in your config to use workers by default.

Tags & Filtering

bench("Parser", fn, { tags: ["parser", "json"] });
benched run --tag parser

Export

Export results to JSON or CSV with optional environment metadata:

benched run array.bench.ts --export json
benched run array.bench.ts --export csv

JSON exports include runtime, OS, CPU, memory, timestamp, and optional git commit hash.

Environment Information

Benched automatically captures:

  • Node/Bun/Deno version
  • Operating system and architecture
  • CPU model
  • Total memory
  • Timestamp
  • Git commit (when available)

CI Usage

- name: Run benchmarks
  run: pnpm benched run --no-ui --export json

- name: Compare against baseline
  run: pnpm benched compare baseline.json benched-results-*.json

Use --no-ui in CI environments that do not support interactive terminal output.

Development

pnpm install
pnpm build
pnpm test
pnpm typecheck

Run the example benchmark:

pnpm build
node dist/cli/index.js run examples/array.bench.ts

License

MIT © Ethan Davies

About

A benchmarking framework for JavaScript and TypeScript that makes it easy to write, run, analyse, and compare performance benchmarks.

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages