import {Benchmark} from '@fuzdev/fuz_util/benchmark.js'; Benchmark class for measuring and comparing function performance.
constructor
type new (config?: BenchmarkConfig): Benchmark
config
{}add
Add a benchmark task.
type (name: string, fn: () => unknown): this
name
task name or full task object
stringfn
Function to benchmark (if name is string). Return values are ignored.
() => unknownthisthis Benchmark instance for chaining
bench.add('simple', () => fn());
// Or with setup/teardown:
bench.add({
name: 'with setup',
fn: () => process(data),
setup: () => { data = load() },
teardown: () => { cleanup() },
});throws
Error- if a task with the same name already exists, or if `fn` is missing when `name` is a string
remove
Remove a benchmark task by name.
type (name: string): this
name
stringthisthis Benchmark instance for chaining
bench.add('task1', () => fn1());
bench.add('task2', () => fn2());
bench.remove('task1');
// Only task2 remainsthrows
Error- if task with given name doesn't exist
run
Run all benchmark tasks.
Tasks execute in add() order. The first task runs against a colder
runtime than subsequent ones (uncompiled JS, cold caches) — a
property of in-process benchmarking that matters more when an early
task has aggressive overrides like low warmup_iterations or
min_iterations. If first-position bias is a concern, put a
throwaway warm-up task first, or call run() twice and use the
second result set.
type (): Promise<BenchmarkResult[]>
Promise<BenchmarkResult[]>table
Format results as an ASCII table with percentiles, min/max, and relative performance.
type (options?: BenchmarkFormatTableOptions | undefined): string
options?
BenchmarkFormatTableOptions | undefinedstring// Standard table
console.log(bench.table());
// Grouped by category
console.log(bench.table({
groups: [
{ name: 'FAST PATHS', filter: (r) => r.name.includes('fast') },
{ name: 'SLOW PATHS', filter: (r) => r.name.includes('slow') },
]
}));markdown
Format results as a Markdown table.
type (options?: BenchmarkFormatTableOptions | undefined): string
options?
formatting options (groups for organized output with optional baselines)
BenchmarkFormatTableOptions | undefinedstringformatted markdown string
// Standard table
console.log(bench.markdown());
// Grouped by category with custom baseline
console.log(bench.markdown({
groups: [
{ name: 'Format', filter: (r) => r.name.startsWith('format/'), baseline: 'format/prettier' },
{ name: 'Parse', filter: (r) => r.name.startsWith('parse/') },
]
}));json
Format results as JSON.
type (options?: BenchmarkFormatJsonOptions | undefined): string
options?
formatting options (pretty, include_timings)
BenchmarkFormatJsonOptions | undefinedstringJSON string
results
Get the benchmark results.
type (): BenchmarkResult[]
BenchmarkResult[]shallow copy of the results array (prevents external mutation)
results_by_name
Get results as a map for convenient lookup by task name.
type (): Map<string, BenchmarkResult>
Map<string, BenchmarkResult>fresh Map of task name to benchmark result (prevents external mutation)
const results_map = bench.results_by_name();
const slugify_result = results_map.get('slugify');
if (slugify_result) {
console.log(`slugify: ${slugify_result.stats.ops_per_second} ops/sec`);
}reset
Reset the benchmark results. Keeps tasks intact so benchmarks can be rerun.
type (): this
thisthis Benchmark instance for chaining
clear
Clear everything (results and tasks).
type (): this
thisthis Benchmark instance for chaining
summary
Get a quick text summary of the fastest task.
type (): string
stringhuman-readable summary string
console.log(bench.summary());
// "Fastest: slugify_v2 (1,285,515.00 ops/sec, 786.52ns per op)"
// "Slowest: slugify (252,955.00 ops/sec, 3.95μs per op)"
// "Speed difference: 5.08x"has_results
Check if the benchmark has been run and has results.
type boolean
if (bench.has_results) {
console.log(bench.table());
}