Benchmarks JS functions using the builtin perf_hooks. Can benchmark several functions at the same time for multiple times, and returns stats on runtimes.
npm install @darylt/benchmarkerversion/vanilla-js branch. It uses Node's built-in perf_hook library to measure a function's completion time. To get readings that are more accurate, the number of times a function runs can be specified using the cycles argument.
npm install @darylt/benchmarker in your npm project folder.
npm install to install both dependencies.
AnonymousFuncN, where N is the index position of that function in the callback array.
benchmark(functionsToRun, cycles) function takes in two arguments.
functionsToRun - an array containing all the functions to benchmark.
cycles - an integer value that determines how many times to run each function.
const { benchmark } = require('path_to/benchmark.js') near the start of the script, then call benchmark() as described above.
benchmark() does not prevent any functional side-effects. If your function's side effects depends on external factors, runtimes may differ.
let status = true;
// ...
const exampleFunc = function() {
if (status) {
let i = 0;
while (i < 1000000000) {
i++;
}
status = false;
}
};
results = benchmark([exampleFunc], 100);
`
The first time exampleFunc runs, it will execute the while loop. However, any subsequent runs will not because status has been set to false. Therefore the first reported completion time will be much higher than all subsequent runs of this function. To check the logged completion times, use results.exampleFunc.perfHistory or results[0].perfHistory. Use the latter if you passed in anonymous functions, or use its automatically-assigned name AnonymousFuncN`, where N is the index of that function in the callback function array.