On this page

    M

    run

    History
    run(options?): BenchmarksStream
    Attributes
    options:Object
    namePattern:string | RegExp
    Only runs benchmarks whose full hierarchical name matches the pattern. String values are interpreted as JavaScript regular expressions.
    samples:number
    Overrides the maximum number of measured callback invocations for every benchmark. Must be a positive 32-bit unsigned integer.
    Allows aborting in-progress benchmark execution.
    warmup:number
    Overrides the number of unreported warmup callback invocations for every benchmark. Must be a 32-bit unsigned integer.
    yieldBetweenSamples?:boolean
    Schedule an event loop turn between sample callbacks. Default: true, or the value passed to createRunner() for an explicit runner.
    Returns:BenchmarksStream

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