Skip to content

benchmark

The benchmark function measures the average execution time of a synchronous or asynchronous function, useful for performance analysis.

Syntax

typescript
await benchmark(callback, iterations)

Parameters

NameTypeDescription
callbackFunctionFunction to be executed and measured (can be asynchronous).
iterationsnumberNumber of executions to calculate the average (>= 1).

Return Value

TypeDescription
Promise<number>Average execution time in milliseconds.

Examples

typescript
await benchmark(() => fetch('/api/data'), 10); // Measures 10 executions

Notes

  • Throws a TypeError if the callback is not a function or iterations is not a valid number.
  • Useful for comparing the performance of different implementations.

References

Released under the MIT License.