@deepkit/bench

中文文档
1.1.0发布于 2026/11/01MITTypeScriptESM + CJS零依赖

Deepkit Bench

原始使用文档
文档目录12 节
  1. Features
  2. Installation
  3. Simple API
  4. BenchSuite API
  5. Output
  6. Formatting Utilities
  7. Visualization Utilities
  8. Memory & GC Utilities
  9. API Reference
  10. Types
  11. BenchSuite
  12. Tips for Accurate Benchmarks

@deepkit/bench

Zero-dependency benchmark utility with visualization and statistical analysis.

Features

  • Automatic sync/async detection
  • Adaptive iteration selection (1x to 10M iterations)
  • GC event tracking (when --expose-gc is used)
  • Heap delta measurement
  • Statistical analysis (RME, variance)
  • Color-coded output with block bar visualization
  • Both simple API and class-based BenchSuite API

Installation

npm install @deepkit/bench

Simple API

For quick, one-off benchmarks:

import { benchmark, run } from '@deepkit/bench';

let counter = 0;

benchmark('increment', () => {
  counter += 10;
});

benchmark('multiply', () => {
  counter *= 2;
});

await run(1); // Run each benchmark for 1 second

Run with:

node --import @deepkit/run benchmarks/test.ts

BenchSuite API

For organized benchmark suites with comparison:

import { BenchSuite } from '@deepkit/bench';

const suite = new BenchSuite('serialization', 1, true); // name, maxTime, showSummary

suite.add('serialize', () => {
  serialize<User>(user);
});

suite.add('deserialize', () => {
  deserialize<User>(data);
});

await suite.runAsync();

Output

━━━ serialization ━━━

  ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁▂▃▄▅▆   32,456,789.12 ops/sec    30.821 ns/op   ±1.23%  serialize
  ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁▂▃▄▅▆   28,123,456.78 ops/sec    35.557 ns/op   ±0.98%  deserialize

  Summary:
  ███████████████   32.46M ops/sec fastest  serialize
  █████████████░░   28.12M ops/sec   x1.15  deserialize

Formatting Utilities

import { formatBytes, formatHz, formatHzFull, formatMean, formatRme } from '@deepkit/bench';

formatHz(1_000_000); // "1.00M"
formatHzFull(1_000_000); // "1,000,000.00"
formatMean(0.000001); // "1.000 ns"
formatMean(0.001); // "1.000 µs"
formatMean(1); // "1.000 ms"
formatRme(1.5); // "±1.50%"
formatBytes(1024); // "1.00 KB"

Visualization Utilities

import { getBlockBar, getBrailleBar } from '@deepkit/bench';

// Block bar (uses █▇▆▅▄▃▂▁ characters with HSV color gradient)
getBlockBar(samples, 20);

// Braille bar (more compact, uses Braille characters)
getBrailleBar(samples);

Memory & GC Utilities

import { forceGC, getHeapUsage, warmup, warmupAsync } from '@deepkit/bench';

// Force garbage collection (requires --expose-gc)
forceGC();

// Get current heap usage in bytes
const heap = getHeapUsage();

// Warmup a function for V8 optimization
warmup(myFunction, 100);

// Warmup an async function
await warmupAsync(myAsyncFunction, 50);

API Reference

Types

interface BenchResult {
  hz: number; // Operations per second
  elapsed: number; // Total elapsed time in seconds
  rme: number; // Relative margin of error (percentage)
  mean: number; // Mean time per operation in milliseconds
  variance: number; // Variance of samples
  iterations: number; // Number of iterations run
  samples: number[]; // Sample timings (up to 50)
  heapDiff: number; // Heap memory difference in bytes
  async: boolean; // Whether this is an async benchmark
  gcEvents: number[]; // GC events during benchmark (pause times in ms)
}

type BenchSuiteResult = { [name: string]: BenchResult };

interface BenchmarkOptions {
  maxTime?: number; // Maximum time to run in seconds (default: 1)
}

BenchSuite

class BenchSuite {
  static onComplete?: (name: string, result: BenchSuiteResult) => void;

  constructor(
    name: string,
    defaultMaxTime?: number, // Default: 1
    showSummary?: boolean, // Default: false
  );

  add(name: string, fn: () => void | Promise<void>, options?: BenchmarkOptions): this;
  run(options?: { verbose?: boolean }): void;
  runAsync(options?: { verbose?: boolean }): Promise<BenchSuiteResult>;
  getResults(): BenchSuiteResult;
}

Tips for Accurate Benchmarks

  1. Use --expose-gc for GC tracking: node --expose-gc script.ts
  2. Warmup functions before benchmarking to ensure V8 optimization
  3. Avoid side effects in benchmark functions that could skew results
  4. Run multiple times to account for system variance
  5. Use longer durations (2-5 seconds) for more stable results

版本与发布记录

@deepkit/bench 共发布 5 个版本,首次发布于 2025/04/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.0latest2026/11/012 天前
1.0.192025/09/221.1 年前
1.0.182025/09/221.1 年前
1.0.152025/06/261.4 年前
1.0.52025/04/031.6 年前

关于 @deepkit/bench 的常见问题

@deepkit/bench 是什么?

Deepkit板凳

如何安装 @deepkit/bench?

在项目目录运行 npm install @deepkit/bench,或使用 pnpm add / yarn add / bun add。

@deepkit/bench 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deepkit/bench 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@deepkit/bench 可以商用吗?

@deepkit/bench 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deepkit/bench 还在维护吗?

最新版本 1.1.0 发布于 2026/11/01(2 天前);至今共发布 5 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/04/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。