Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Latest commit

 

History

History
146 lines (103 loc) · 3.65 KB

File metadata and controls

146 lines (103 loc) · 3.65 KB

@funish/bench

npm version npm downloads npm license Contributor Covenant

High-performance benchmarking utilities for Node.js and browser environments, powered by Funish.

Features

  • 🚀 Simple and intuitive API
  • 📊 Comprehensive statistical analysis
  • ⏱️ High-resolution timing
  • 🔄 Multiple time units support (s, ms, µs, ns)
  • 🌐 Works in Node.js and browsers
  • 📈 Detailed performance metrics
  • 📦 Zero dependencies
  • 🌟 Full TypeScript support

Installation

# npm
$ npm install @funish/bench

# yarn
$ yarn add @funish/bench

# pnpm
$ pnpm add @funish/bench

Usage

Basic Benchmarking

import { Bench } from "@funish/bench";

const bench = new Bench({
  times: 1000, // Number of iterations
  unit: "ms", // Time unit (s, ms, µs, ns)
});

// Add tasks to benchmark
bench.add("Array.push", () => {
  const arr = [];
  arr.push(1);
});

bench.add("Array literal", () => {
  const arr = [1];
});

// Print results
bench.print();

Chaining API

new Bench()
  .add("Task 1", () => {
    /* ... */
  })
  .add("Task 2", () => {
    /* ... */
  })
  .print();

Getting Results Programmatically

const bench = new Bench();
bench.add("test", () => {
  /* ... */
});

const results = bench.getResults();
// Process results as needed

API Reference

Bench Class

Constructor

new Bench(options?: BenchOptions)
Options
  • times (number, default: 1000): Number of iterations for each task
  • unit ("s" | "ms" | "µs" | "ns", default: "ns"): Time unit for results

Methods

add(name: string, fn: () => void): Bench

Adds a task to benchmark.

  • name: Task identifier
  • fn: Function to benchmark
  • Returns: The Bench instance for chaining
print(): void

Prints results to console in a table format.

getResults(): readonly BenchResult[]

Returns a copy of the benchmark results.

Result Format

Each result contains:

  • Task name
  • Total time
  • Average time
  • Fastest time
  • Slowest time
  • Median time
  • Standard deviation

Example output:

┌─────────┬────────────┬─────────────┬─────────────┬─────────────┬─────────────┬────────────────────┐
│ Task    │ Total (ms) │ Average (ms)│ Fastest (ms)│ Slowest (ms)│ Median (ms) │ Standard deviation │
├─────────┼────────────┼─────────────┼─────────────┼─────────────┼─────────────┼────────────────────┤
│ Task 1  │ 100.000    │ 0.100       │ 0.080       │ 0.150       │ 0.095       │ 0.015             │
└─────────┴────────────┴─────────────┴─────────────┴─────────────┴─────────────┴────────────────────┘

Contributing

Please read our Contributing Guide before submitting a Pull Request to the project.

License

MIT © Funish