可重复使用的长条垫


🌐 Reusable runners

模块级声明函数使用共享运行器并自动调度它。更高级的工具可以创建独立的、显式启动的运行器,如下所示:

🌐 The module-level declaration functions use a shared runner and schedule it automatically. Higher-level tools can create isolated, explicitly started runners instead:

import { createRunner } from 'node:bench';

const runner = createRunner({ yieldBetweenSamples: false });

runner.bench('example', { samples: 100 }, (b) => {
  const operations = chooseOperationCount();
  b.start();
  runOperations(operations);
  const sample = b.end(operations);

  if (hasEnoughData(sample)) b.done();
});

for await (const record of runner.run()) {
  // Consume structured benchmark records.
} 

每个运行器都有独立的声明、钩子、过滤和输出。不像模块级声明,在一个明确的运行器上创建基准测试并不会安排执行。这允许包收集声明并稍后启动它们。调用明确运行器的run()函数会阻止额外的声明,而第二次调用run()则会报错。

🌐 Each runner has independent declarations, hooks, filtering, and output. Unlike the module-level declarations, creating a benchmark on an explicit runner does not schedule execution. This allows packages to collect declarations and start them later. Calling the explicit runner's run() function prevents additional declarations and a second call to run() is an error.