@chainsafe/benchmark

中文文档
2.1.0发布于 2026/09/18MITES Module构建来源已验证近 30 天下载 1.5万

This is an independently maintained fork of @dapplion/benchmark. This repo now maintains it's own versioning as @chainsafe/benchmark and release schedule. It was forked from the base of `@dapplion/benchmark@1

原始使用文档
文档目录22 节
  1. Quick start
  2. How does it work?
  3. Track performance in CI
  4. Track performance locally
  5. Isolate benchmark files (Node.js)
  6. Config
  7. --isolate
  8. --defaultBranch
  9. --persistBranches
  10. --benchmarksPerBranch
  11. --threshold
  12. --compareBranch
  13. --compareCommit
  14. --prune
  15. --persist
  16. --noThrow
  17. --historyLocal , --local
  18. --historyGaCache , --ga-cache
  19. --historyS3 , --s3
  20. Roadmap
  21. Compare performance
  22. Add more providers

Benchmark

This is an independently maintained fork of @dapplion/benchmark. This repo now maintains it's own versioning as @chainsafe/benchmark and release schedule. It was forked from the base of @dapplion/[email protected] and new functionality was added.

Ensures that new code does not introduce performance regressions with CI. Tracks:

  • Do PR against the base branch include a performance regression?
  • Do new commits in the main branch include a performance regression?

This tooling provides both a easy to use runner for benchmarking and easy integrations to persist past benchmark data.

Quick start

Create a benchmark test file but use itBench instead of it

import {describe, itBench, setBenchOpts} from "@chainsafe/benchmark";

describe("Sum array benchmark", () => {
  itBench("sum array with reduce", () => {
    arr.reduce((total, curr) => total + curr, 0);
  });
});

Then run the CLI.

benchmark 'test/perf/**/*.perf.ts' --local

Inspect benchmark results in the terminal

  Sum array benchmark
    ✔ sum array with reduce                                               826.0701 ops/s    1.210551 ms/op   x0.993        578 runs   1.21 s

How does it work?

This tool is a CLI tool, example usage:

benchmark 'test/perf/**/*.perf.ts' --s3

The above command will:

  • Read benchmark history from the specified provider (AWS S3)
  • Figure out the prev benchmark based on your option (defaults to latest commit in main branch)
  • Run benchmark comparing with previous
    • Collect benchmark data in-memory while streaming results
  • Add result to benchmark history and persist them to the specified provider (AWS S3)
  • If in CI, post a PR or commit comment with an expandable summary of the benchmark results comparision
  • If a performance regression was detected, exit 1

Track performance in CI

Below is a suggested Github action to run this tool with s3 history provider:

name: Benchmark
# Ensure a single benchmark is run at a time
concurrency: cd-benchmark-${{ github.ref }}

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  s3:
    if: always()
    runs-on: ubuntu-latest
    # Ensure both don't run at the same time
    needs:
      - local
      # - ga-cache

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2-beta
      - run: yarn install --frozen-lockfile

      # Run benchmark with custom tooling and stores the output to a file
      - name: Run performance tests
        run: yarn benchmark --s3
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          # S3 credentials
          S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }}
          S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }}
          S3_REGION: ${{ secrets.S3_REGION }}
          S3_BUCKET: ${{ secrets.S3_BUCKET }}
          S3_ENDPOINT: ${{ secrets.S3_ENDPOINT }}
          # Key prefix to separate benchmark data from multiple repositories
          S3_KEY_PREFIX: ${{ github.repository }}/${{ runner.os }}

Track performance locally

When working on optimizing a function you may want to know if your code is actually faster than the previous implementation.

To do that you can keep a benchmark history file locally and run the benchmark first against previous code

git checkout master
benchmark test/perf/func.perf.ts --local
  • Runs benchmark without comparing with previous
  • Writes single benchmark data to ./benchmark_data

Then measure performance with the new code

git checkout fix1
benchmark test/perf/func.perf.ts --local
  • Reads single benchmark data from ./benchmark_data
  • Run benchmark comparing with prev
  • Does not write benchmark data

Isolate benchmark files (Node.js)

Use --isolate to run each file sequentially in a fresh process instead of the default shared process:

benchmark 'test/perf/**/*.perf.js' --isolate --historyLocal ./benchmark_data-isolated

Each worker inherits the Node executable, runtime flags (including loaders and --expose-gc), environment, and working directory. Setup files run in each worker. The next file starts only after the previous worker exits, releasing its retained memory and native resources. Completed workers exit even if benchmark code leaves open handles.

The parent still owns history, the combined snapshot, performance comparison, and the single GitHub report. Collection failures, failed benchmarks, crashes, missing worker results, interruption, and duplicate IDs across files abort before persistence, including with --noThrow. Skipped files are allowed, but the complete run must produce at least one result.

Files must be independent: globals and hooks are not shared across files, and .only selection is local to each file. Use unique benchmark IDs across files. When switching modes, use a fresh local history path, CI cache key, or S3 prefix; shared-process and isolated measurements are not interchangeable baselines. History namespaces are not changed automatically.

Config

--isolate

Run each file sequentially in a fresh process

  • type: boolean
  • default: false

--defaultBranch

Provide the default branch of this repository to prevent fetching from Github

  • type: string
  • default:

--persistBranches

Choose what branches to persist benchmark data

  • type: array
  • default: default-branch

--benchmarksPerBranch

Limit number of benchmarks persisted per branch

  • type: number
  • default: Infinity

--threshold

Ratio of new average time per run vs previos time per run to consider a failure. Set to 'Infinity' to disable it.

  • type: number
  • default: 2

--compareBranch

Compare new benchmark data against the latest available benchmark in this branch

  • type: string
  • default: default-branch

--compareCommit

Compare new benchmark data against the benchmark data associated with a specific commit

  • type: string
  • default:

--prune

When persisting history, delete benchmark data associated with commits that are no longer in the current git history

  • type: boolean
  • default:

--persist

Force persisting benchmark data in history

  • type: boolean
  • default:

--noThrow

Exit cleanly even if a preformance regression was found

  • type: boolean
  • default:

--historyLocal, --local

Persist benchmark history locally. May specify just a boolean to use a default path, or provide a path

  • type: string
  • default: ./benchmark_data

--historyGaCache, --ga-cache

Persist benchmark history in Github Actions cache. Requires Github authentication. May specify just a boolean to use a default cache key or provide a custom key

  • type: string
  • default: benchmark_data

--historyS3, --s3

Persist benchmark history in an Amazon S3 bucket. Requires Github authentication

  • type: string
  • default:

Roadmap

Compare performance

bench compare --from <branch-name | commit-hash> --to <branch-name | commit-hash>
  • Retrieves benchmark history
  • Don't run benchmark
  • Print comparision of from benchmark with to benchmark

Add more providers

  • Github actions cache: Doesn't work due to Github's limitations
  • Commited in the repository (gh-pages, or data branch)

版本与发布记录

@chainsafe/benchmark 共发布 16 个版本,首次发布于 2025/01/10;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2026/09/181 个月前
2.0.22026/03/307 个月前
2.0.12025/05/011.5 年前
2.0.02025/04/301.5 年前
1.2.32025/01/301.8 年前
1.2.22025/01/291.8 年前

关于 @chainsafe/benchmark 的常见问题

@chainsafe/benchmark 是什么?

This is an independently maintained fork of @dapplion/benchmark. This repo now maintains it's own versioning as @chainsafe/benchmark and release schedule. It was forked from the base of `@dapplion/benchmark@1

如何安装 @chainsafe/benchmark?

在项目目录运行 npm install @chainsafe/benchmark,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 benchmark,可以用 npx benchmark 直接运行。

@chainsafe/benchmark 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/chainsafe__benchmark 是否存在。

@chainsafe/benchmark 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chainsafe/benchmark 可以商用吗?

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

@chainsafe/benchmark 的下载量有多少?

根据 npm 官方下载统计,@chainsafe/benchmark 在 2026/10/10 – 2026/11/08 这 30 天内被下载 15,324 次,日均约 511 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chainsafe/benchmark 还在维护吗?

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