Skip to content

Instantly share code, notes, and snippets.

@DimaCrafter
Last active October 26, 2019 09:34
Show Gist options
  • Save DimaCrafter/d68dfd0dab67e5b38cac2ea8dfba1146 to your computer and use it in GitHub Desktop.
Save DimaCrafter/d68dfd0dab67e5b38cac2ea8dfba1146 to your computer and use it in GitHub Desktop.
[JS] Benchmarking functions

Usage:

test([
    () => expr1,
    () => expr2,
    // ...
    () => exprN
], <how many times execute>);
function test (ways, times) {
ways.forEach((fn, k) => {
let total = 0;
for (let i = 0; i < times; i++) {
const start = performance.now();
fn();
const end = performance.now();
total += end - start;
}
console.log(k + ': ' + (total / times).toFixed(5) + 'ms');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment