Files
30-seconds-of-code/snippets/measure-time-taken-by-function.md
2017-12-15 08:43:47 +11:00

15 lines
418 B
Markdown

### Measure time taken by function
Use `console.time()` and `console.timeEnd()` to measure the difference between the start and end times to determine how long the callback took to execute.
```js
const timeTaken = callback => {
console.time('timeTaken');
const r = callback();
console.timeEnd('timeTaken');
return r;
};
// timeTaken(() => Math.pow(2, 10)) -> 1024
// (logged): timeTaken: 0.02099609375ms
```