Files
30-seconds-of-code/snippets/measure-time-taken-by-function.md
Angelos Chalaris 43f8529cb9 Added samples
2017-12-12 17:50:08 +02:00

446 B

Measure time taken by function

Use performance.now() to get start and end time for the function, console.log() the time taken. First argument is the function name, subsequent arguments are passed to the function.

const timeTaken = (f,...args) => {
  var t0 = performance.now(), r = f(...args);
  console.log(performance.now() - t0);
  return r;
}
// timeTaken(Math.pow, 2, 10) -> 1024 (0.010000000009313226 logged in console)