console.time
Measures how long code took to run.
Available since: Web API
Syntax
console.time(label) … console.timeEnd(label)Parameters
| Parameter | Type | Description |
|---|---|---|
| label | string | Optional. The timer name; timeEnd must use the same one. |
Return value
undefinedReturns nothing.
Examples
Timing a loop (output in the browser console):
console.time('loop');
let sum = 0;
for (let i = 0; i < 1_000_000; i++) sum += i;
console.timeEnd('loop');Output:
loop: 3.27 msNotes
The time differs on every run. console.timeLog(label) prints the intermediate time without stopping the timer. To measure in code use performance.now() or Date.now().
See also
- Date.now — Returns the current time as a number of milliseconds.
- console.count — Counts how many times this line was called.
Related recipes
In the tutorial
- Debugging — How to find bugs in JavaScript: reading the error message, console.log and its variants, checking types, developer tools and debugger.