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.
The time()
method starts a timer in the console view.
The time()
method allows you to time code for testing purposes.
You can run many timers at the same time.
Use the label parameter to name different timers.
console.time(
label)
Parameter | Description |
label | Optional. A label to give the timer a name. |
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 »
console.time()
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!