@c4312/matcha

中文文档
1.3.1发布于 2020/07/09MITCommonJS近 30 天下载 5583

A caffeine driven, simple command line for benchmarking

原始使用文档
文档目录6 节
  1. Usage
  2. Command-Line Options
  3. Async Benchmarks
  4. Settings
  5. Nested Suites
  6. API

@c4312/matcha

A modernization of matcha, powered by Benchmark.js. I found Matcha super useful over the years, but it has issues with accuracy, doesn't support promises or work with Node 12, and is apparently abandoned. We fix those here!

Demonstration video of the matcha command line

Usage

npm install --global @c4312/matcha

Then you can create a file and bench functions, for instance if you have a my-bench.js:

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

bench('forEach', () => arr.forEach(v => (sum += v)));

bench('for-of loop', () => {
  for (const v of arr) {
    sum += v;
  }
});

Then, simply run:

matcha my-bench.js

Command-Line Options

Usage: matcha [options] <file>

Options:
  -V, --version              output the version number
  -g, --grep <pattern>       Run a subset of benchmarks (default: "")
  -R, --reporter <reporter>  Specify the reporter to use (default: "pretty")
  --cpu-profile [pattern]    Run on all tests, or those matching the regex.
                             Saves a .cpuprofile file that can be opened in the Chrome devtools.
  --reporters                Display available reporters
  -h, --help                 output usage information

Async Benchmarks

You can return promises from your benchmarks and take callbacks:

bench('plain fs', callback => readFile(__filename, callback));
bench('promisifed fs', async () => await readFileAsync(__filename));

Settings

You can set any benchmark.js option via the set() helper. You can also have async setup and teardown methods.

// in a promise:
set('setup', async () => {
  await waitUntilPageIsLoaded();
});

// or a callback:
set('teardown', callback => closePage(callback));

// as well as base options:
set('maxTime', 1);
set('minSamples', 2000);

Nested Suites

Multiple benchmark suites can be nested. Options, including setup and teardown, are inherited, chained, and overridden.

set('setup', globalPrepare);

// globalPrepare() run before:
bench('a', runA);

// globalPrepare() and nestedPrepare() run before
// these, and they run for 1 second at most:
suite('nested', () => {
  set('setup', nestedPrepare);
  set('maxTime', 1);
  bench('b', runB);
});

API

You can use the Matcha API programmatically:

const { GatherReporter, benchmark } = require('@c4312/benchmark');

// A reporter that just stores results in an array:
const reporter = new GatherReporter();

await benchmark({
  reporter,
  prepare(api) {
    // The standard API as described above!
    api.bench('myFunction', fn);

    // This function may be async and return a promise,
    // which we'll wait for before we start benchmarking.
  },
});

for (const result of reporter.results) {
  console.log('Benchmark', result.name, 'runs at', result.hz, 'ops/sec');
}

版本与发布记录

@c4312/matcha 共发布 5 个版本,首次发布于 2019/12/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.1latest2020/07/096 年前
1.3.02020/05/276 年前
1.2.02020/01/126 年前
1.1.02020/01/116 年前
1.0.02019/12/236 年前

关于 @c4312/matcha 的常见问题

@c4312/matcha 是什么?

A caffeine driven, simple command line for benchmarking

如何安装 @c4312/matcha?

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

@c4312/matcha 支持 TypeScript 吗?

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

@c4312/matcha 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@c4312/matcha 可以商用吗?

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

@c4312/matcha 的下载量有多少?

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

@c4312/matcha 还在维护吗?

最新版本 1.3.1 发布于 2020/07/09(6 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/12/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。