@c4312/matcha

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

咖啡因驱动的简单基准命令行

中文使用文档
文档目录6 节
  1. 使用量
  2. 命令行选项
  3. 同步基准
  4. 设置
  5. 嵌入式套装
  6. API 密码

@c4312/马查

现代化 matcha,由基准.js提供动力. 这些年我发现Matcha超级有用,但它已经 准确的问题,不支持承诺 或与节点12, 而且显然被抛弃。 我们在这里解决!

Demonstration video of the matcha command line

使用量

npm install --global @c4312/matcha

然后,你可以创建一个文件 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;
  }
});

然后,简单运行:

matcha my-bench.js

命令行选项

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

同步基准

你可以从基准返回承诺 并收回召回:

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

设置

您可以设置任何 基准.js 选项 通过 set() 帮手 也可以采用Aync设置和撕裂方法.

// 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);

嵌入式套装

多个基准套房可以被嵌入. 包括设置和拆卸在内的选项是继承的、被锁住和被推翻的.

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 密码

您可以在程序上使用 Matcha API :

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 是什么?

咖啡因驱动的简单基准命令行

如何安装 @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 和提交记录判断。