benchmark.ts

Benchmarking library.

@example ```ts import {Benchmark} from '@fuzdev/fuz_util/benchmark.js';

const bench = new Benchmark({ duration_ms: 5000, warmup_iterations: 5, });

bench .add('slugify', () => slugify(title)) .add('slugify_slower', () => slugify_slower(title));

const results = await bench.run(); console.log(bench.table()); ```

Declarations
#

2 declarations

view source

Benchmark
#

benchmark.ts view source

Benchmark class for measuring and comparing function performance.

constructor

type new (config?: BenchmarkConfig): Benchmark

config
default {}

add

Add a benchmark task.

type (name: string, fn: () => unknown): this

name

- Task name or full task object

type string
fn

- Function to benchmark (if name is string). Return values are ignored.

type () => unknown
returns this

This Benchmark instance for chaining

add

type (name: string, fn: () => unknown): this

name
type string
fn
type () => unknown
returns this

add

type (name: string, fn: () => unknown): this

name
type string
fn
type () => unknown
returns this

remove

Remove a benchmark task by name.

type (name: string): this

name

- Name of the task to remove

type string
returns this

This Benchmark instance for chaining

throws
  • Error - if task with given name doesn't exist

skip

Mark a task to be skipped during benchmark runs.

type (name: string): this

name

- Name of the task to skip

type string
returns this

This Benchmark instance for chaining

throws
  • Error - if task with given name doesn't exist

only

Mark a task to run exclusively (along with other only tasks).

type (name: string): this

name

- Name of the task to run exclusively

type string
returns this

This Benchmark instance for chaining

throws
  • Error - if task with given name doesn't exist

run

Run all benchmark tasks.

type (): Promise<BenchmarkResult[]>

returns Promise<BenchmarkResult[]>

Array of benchmark results

table

Format results as an ASCII table with percentiles, min/max, and relative performance.

type (options?: BenchmarkFormatTableOptions | undefined): string

options?

- Formatting options

type BenchmarkFormatTableOptions | undefined
optional
returns string

Formatted table string

markdown

Format results as a Markdown table.

type (options?: BenchmarkFormatTableOptions | undefined): string

options?

- Formatting options (groups for organized output with optional baselines)

type BenchmarkFormatTableOptions | undefined
optional
returns string

Formatted markdown string

json

Format results as JSON.

type (options?: BenchmarkFormatJsonOptions | undefined): string

options?

- Formatting options (pretty, include_timings)

type BenchmarkFormatJsonOptions | undefined
optional
returns string

JSON string

results

Get the benchmark results. Returns a shallow copy to prevent external mutation.

type (): BenchmarkResult[]

returns BenchmarkResult[]

Array of benchmark results

results_by_name

Get results as a map for convenient lookup by task name. Returns a new Map each call to prevent external mutation.

type (): Map<string, BenchmarkResult>

returns Map<string, BenchmarkResult>

Map of task name to benchmark result

reset

Reset the benchmark results. Keeps tasks intact so benchmarks can be rerun.

type (): this

returns this

This Benchmark instance for chaining

clear

Clear everything (results and tasks). Use this to start fresh with a new set of benchmarks.

type (): this

returns this

This Benchmark instance for chaining

summary

Get a quick text summary of the fastest task.

type (): string

returns string

Human-readable summary string

benchmark_warmup
#

benchmark.ts view source

(fn: () => unknown, iterations: number, async_hint?: boolean | undefined): Promise<boolean>

Warmup function by running it multiple times. Detects whether the function is async based on return value.

fn

- Function to warmup (sync or async)

type () => unknown

iterations

- Number of warmup iterations

type number

async_hint?

- If provided, use this instead of detecting

type boolean | undefined
optional

returns

Promise<boolean>

Whether the function is async

examples

Example 1

Depends on
#