API @deepkit/bench
npm install @deepkit/bench
コードスニペットをベンチマークするためのシンプルなツール。
import { benchmark, run } from '@deepkit/bench'; // ASCII バイナリパースの例 const binaryString = Buffer.from('Hello World', 'utf8'); const codes = [ benchmark('Buffer.toString', () => { const utf8String = binaryString.toString('utf8'); }); benchmark('String.fromCodePoint', () => { const utf8String = String.fromCodePoint() }); void run();
$ node --import @deepkit/run benchmarks/ascii-parsing.ts Node v22.13.1 🏎 x 20,326,482.53 ops/sec ± 4.95% 0.000049 ms/op ▆▆▇▅▆▆▅▅▆▅▅▅▅▅▅▅▅▅▅▅▅▅ Buffer.toString 19850001 samples 🏎 x 36,012,545.69 ops/sec ± 1.78% 0.000028 ms/op ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ String.fromCodePoint 35800001 samples done
BenchSuite - A benchmark suite for organizing and running multiple benchmarks. Features: Creates a colored block bar visualization from sample data.
Uses HSV color gradient (green -> yellow) based on value. Creates a Braille-based bar visualization (more compact). Formats operations per second with K/M/B suffix. Formats operations per second with full number and locale formatting. Formats mean time per operation with appropriate unit (ns, µs, ms, s). Formats relative margin of error as percentage. Formats bytes with appropriate unit (B, KB, MB). Registers a benchmark with the given name and function.
Function can be synchronous or asynchronous. Runs all registered benchmarks each for the given number of seconds. Forces garbage collection if --expose-gc flag was used. Gets current heap memory usage in bytes. Warmup function for V8 optimization.
Run a function multiple times to ensure V8 optimizes it before benchmarking. Async warmup function for V8 optimization. Result of a single benchmark Results of a benchmark suite (name -> result) Options for individual benchmarksClasses
export class BenchSuite {
/** Global callback when any suite completes */
static onComplete?: (name: string, result: BenchSuiteResult) => void;
/** Name of the benchmark suite */
constructor(public readonly name: string, private defaultMaxTime: number = , private showSummary: boolean = false);
/**
* Add a benchmark to the suite.
*
* @param name - Name of the benchmark
* @param fn - Function to benchmark (can be sync or async)
* @param options - Optional settings
*/
add(name: string, fn: () => void | Promise<void>, options: BenchmarkOptions = {}): this;
/**
* Run all benchmarks synchronously and return results.
* Only works with sync benchmark functions.
* For async benchmarks, use runAsync().
*/
run(options: {
verbose?: boolean;
} = {}): BenchSuiteResult;
/**
* Run all benchmarks asynchronously.
*
* @returns Promise resolving to suite results
*/
async runAsync(options: {
verbose?: boolean;
} = {}): Promise<BenchSuiteResult>;
/**
* Get results as JSON-serializable object.
*/
getResults(): BenchSuiteResult;
}
Functions
(values: number[], width?: number): string
(values: number[]): string
(v: number): string
(v: number): string
(ms: number): string
(v: number): string
(bytes: number): string
(name: string, fn: () => void | Promise<void>): void
(seconds?: number): Promise<void>
(): void
(): number
(fn: () => unknown, times?: number): void
(fn: () => Promise<unknown>, times?: number): Promise<void>
Types
// TYPES
// ══════════════════════════════════════════════════════════════════════════════
/**
* Result of a single benchmark
*/
export interface BenchResult {
/** Operations per second */
hz: number;
/** Total elapsed time in seconds */
elapsed: number;
/** Relative margin of error (as percentage) */
rme: number;
/** Mean time per operation in milliseconds */
mean: number;
/** Variance of samples */
variance: number;
/** Number of iterations run */
iterations: number;
/** Sample timings for visualization (up to 50 samples) */
samples: number[];
/** Heap memory difference in bytes */
heapDiff: number;
/** Whether this is an async benchmark */
async: boolean;
/** GC events during benchmark (pause times in ms) */
gcEvents: number[];
}
type BenchSuiteResult = { [name: string]: BenchResult };
interface BenchmarkOptions {
/** Maximum time to run in seconds (default: 1) */
maxTime?: number;
/** Runs before every iteration and is not timed. Async benchmarks only, which then run one iteration at a time. */
beforeEach?: () => Promise<void>;
/** Runs after every iteration and is not timed. Async benchmarks only, which then run one iteration at a time. */
afterEach?: () => Promise<void>;
}