run([options])
options<Object>namePattern<string> | <RegExp> 只运行完整层级名称与模式匹配的基准测试。字符串值会被解释为 JavaScript 正则表达式。samples<number> 覆盖每个基准测试的最大测量回调调用次数。必须是正的 32 位无符号整数。signal<AbortSignal> 允许中途终止正在进行的基准测试。warmup<number> 覆盖每个基准的未报告预热回调调用次数。必须是32位无符号整数。yieldBetweenSamples<boolean> 在采样回调之间安排事件循环的一个周期。默认值:true,或者对于显式运行器,使用传递给createRunner()的值。
- 返回:BenchmarksStream
返回用于进程内基准运行的对象模式事件流。在声明基准的同一轮中调用 run(),在自动执行开始之前。如果不需要返回的流,可以选择不调用 run()。由 createRunner() 创建的显式运行器不会自动运行,因此其 run() 函数可以稍后调用。
🌐 Returns the object-mode event stream for the in-process benchmark run. Call
run() during the same turn in which benchmarks are declared, before automatic
execution begins. Calling run() is optional when the returned stream is not
needed. An explicit runner created by createRunner() does not run
automatically, so its run() function may be called later.
import { bench, run } from 'node:bench';
bench('example', { samples: 3 }, (b) => {
b.start();
doWork();
b.end(1);
});
for await (const { type, data } of run()) {
if (type === 'bench:complete' && data.error === undefined) {
console.log(data.name, data.summary.mean);
}
}