Files
30-seconds-of-code/snippets/timeTaken.md
2017-12-28 08:30:19 +00:00

20 lines
457 B
Markdown

### timeTaken
Measures the time taken by a function to execute.
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;
};
```
```js
timeTaken(() => Math.pow(2, 10)); // 1024
// (logged): timeTaken: 0.02099609375ms
```