Console time()
Example
The time it takes to run a for-loop 100.000 times:
console.time();
for (let i = 0; i < 100000; i++) {
// some code
}
console.timeEnd();
Try it Yourself »
More examples below.
Definition and Usage
The time()
method starts a timer in the console view.
The time()
method allows you to time code for testing purposes.
Note
You can run many timers at the same time.
Use the label parameter to name different timers.
See Also:
Syntax
console.time(label)
Parameters
Parameter | Description |
label | Optional. A label to give the timer a name. |
More Examples
Using the label parameter:
console.time("test1");
for (let i = 0; i < 100000; i++) {
// some code
}
console.timeEnd("test1");
Try it Yourself »
Which is fastest, the for loop or the while loop?
console.time("for loop");
for (let i = 0; i < 1000000; i++) {
// some code
}
console.timeEnd("for loop");
let i = 0;
console.time("while loop");
while (i < 1000000) {
i++
}
console.timeEnd("while loop");
Try it Yourself »
Browser Support
console.time()
is supported in all modern browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 11 | Yes | Yes | Yes | Yes |