bench([name][, options], fn)
name<string> 基准名称。默认:fn的name属性,或者当fn没有名称时为'<anonymous>'。options<Object>diagnosticChannels<Array> 字符串诊断通道名称,通过并集去重并从包含的测试套件继承。数组中的符号值会被自动忽略。默认值:[]。only<boolean> 当任何基准或包含的套件设置了only时,层级中没有only的基准会被跳过。默认值:false。params<Object> 字符串、有限数字或布尔类型元数据,用于标识这个基准配置。在构建稳定的基准身份时,参数键会被排序。**默认值:**一个空对象。samples<number> 测量回调调用的最大次数。必须是一个正的 32 位无符号整数。基准测试可以通过调用context.done()提前结束。默认值:30。signal<AbortSignal> 允许中止此基准测试。skip<boolean> | <string> 如果为真,该基准测试将被跳过。一个字符串会被包含在结果中作为跳过的原因。默认值:false。tags<string[]> 与基准相关的标签。标签会被转换为小写、去重,并通过包含的测试套件以并集的方式继承。默认值:[]。timeout<number> 基准测试失败的毫秒数。默认值:Infinity。warmup<number> 在测量样本之前未报告的回调调用次数。必须是32位无符号整数。默认值:0。
fn<Function> | <AsyncFunction> 基准函数。它接收一个 BenchContext。- 返回:<Promise> 在顶层基准测试完成后,会用基准结果填充,或者在套件中声明
undefined时立即填充。
热身调用使用与测量样本相同的回调和计时契约,但它们的样本会被丢弃。发生异常、拒绝、超时、终止、缺少计时调用或重复计时调用都会停止当前基准测试。后续基准测试仍会继续运行。
🌐 Warmup invocations use the same callback and timing contract as measured samples, but their samples are discarded. An exception, rejection, timeout, abort, missing timing call, or duplicate timing call stops the current benchmark. Later benchmarks continue to run.
在超时或中止之后,运行器会暂时等待异步基准工作的完成,然后再继续。如果它仍然处于挂起状态,所有后续被选中运行的基准测试都会失败而不会运行,以免它们的测量与该工作重叠。
🌐 After a timeout or abort, the runner briefly waits for asynchronous benchmark work to settle before continuing. If it remains pending, all later benchmarks that were selected to run fail without running so that their measurements cannot overlap with that work.
对于每次热身和测量回调,运行器都会订阅配置好的诊断通道。每次发布都会排入一个上下文诊断,其 message 为 { name, message },包含通道名称字符串和发布的消息。当回调完成或被中止时,订阅会被移除。
🌐 For each warmup and measured callback, the runner subscribes to the configured
diagnostics channels. Each publication queues a context diagnostic whose
message is { name, message }, containing the string channel name and the
published message. Subscriptions are removed when the callback settles or is
aborted.
超时或中止无法打断同步的 JavaScript,也不会强制取消忽略 context.signal 的异步工作。
🌐 A timeout or abort cannot interrupt synchronous JavaScript and does not forcibly
cancel asynchronous work that ignores context.signal.
benchId 是基于声明源文件、层级测试套件和基准名称,以及规范化参数生成的。对于同一源位置的重复运行,它是稳定的,但嵌入的源值在不同的检出根目录、模块格式、操作系统或路径大小写之间并未标准化。
🌐 The benchId is based on the declaration source file, hierarchical suite and
benchmark names, and canonicalized parameters. It is stable for repeated runs
from the same source location, but the embedded source value is not normalized
across checkout roots, module formats, operating systems, or path casing.
执行范围是单独表示的。runId 标识一次逻辑运行,而 fileRunId 标识该运行中的文件运行器或子执行。entryFile 字段记录导致声明的入口文件导入,而由预加载模块做出的声明则为 null。因此,当入口文件使用共享声明助手时,相同的 benchId 可以出现在多个 fileRunId 下。在一个文件执行范围内多次声明相同的 benchId 会报错,而不是合并样本。
🌐 Execution scope is represented separately. A runId identifies one logical
run, while fileRunId identifies a file runner or child execution within that
run. The entryFile field records which entry-file import caused a declaration
and is null for declarations made by preload modules.
The same benchId can therefore occur under multiple fileRunId values when
entry files use a shared declaration helper. Declaring the same benchId more
than once within one file execution scope reports an error rather than merging
the samples.