A library to benchmark JavaScript functions with microsecond precision.
Install
npm i function-benchmark-util -D
import { benchmark, benchmarkTimes } from 'function-benchmark-util';
function someFunction(param1, param2) {
// Does something
};
const result1 = benchmark(someFunction, ['param1', 'param2']);
// Running multiple iterations
const result2 = benchmarkTimes(someFunction, 10, ['param1', 'param2']);
// See the results
console.table([result1, result2]);
BenchmarkResult
objectAll benchmark functions, including benchmark
, benchmarkTimes
, benchmarkWithThis
,benchmarkTimesWithThis
return a BenchmarkResult
object.
Property | type & Description |
---|---|
functionName | string Name of the executed function |
runCount | number Execution count passed to benchmarkTimes and benchmarkTimesWithThis functions. |
min | number Minimum duration scored in microseconds |
max | number Maximum duration scored in microseconds |
average | number The average duration of all executions in microseconds |
fnReturns | any Latest returned function result during execution(s) |
this
contextIntentionally benchmark
and benchmarkTimes
functions set this
context of the executed function to null
to avoid ambiguity.
If you need to set this
, use benchmarkWithThis
and for multiple executions, use benchmarkTimesWithThis
.
For more, see the documentation and examples.
MIT © Cengiz Can
TODO: improve docs and examples.
Function to benchmark
Parameters of function as an array
Benchmark a function multiple times with its parameters and sets this context to null
.
Function to benchmark
Times to execute function
Parameters of function as an array
Benchmark a function multiple times with its parameters and sets this context according to passed parameter.
Function to benchmark
Times to execute function
this context for the function
Parameters of function as an array
Benchmark a function only once with its parameters and sets this context according to passed parameter.
Function to benchmark
this context for the function
Parameters of function as an array
Converts microsecond time values to milliseconds in a given BenchmarkResult
object. Does not mutates the input.
Benchmark result object
Precision digits. Default value is 2
Generated using TypeDoc
Benchmark a function only once with its parameters and sets this context to
null
.