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