HTML DOM console.timeEnd() method

Definition and usage

console.timeEnd() method stops the timer and writes the result to the console view.

This method allows you to time certain operations in code for testing.

Please use console.time() method Start the timer.

Please use the label parameter to specify the timer to end.

Tip:When testing console methods, make sure the console view is visible (press F12 to view the console).

Instance

Example 1

How long does it take to execute a for loop 100,000 times?

console.time();
for (i = 0; i < 100000; i++) {
  // some code
}
console.timeEnd();

Try it yourself

Example 2

Use label parameter:

console.time("test1");
for (i = 0; i < 100000; i++) {
  // some code
}
console.timeEnd("test1");

Try it yourself

Example 3

Which is faster, for loop or while loop?

var i;
console.time("test for loop");
for (i = 0; i < 100000; i++) {
  // some code
}
console.timeEnd("test for loop");
i = 0;
console.time("test while loop");
while (i < 1000000) {
  i++
}
console.timeEnd("test while loop");

Try it yourself

Syntax

console.timeEnd(label)

Parameter value

Parameter Type Description
label String Optional. The name of the control to end.

Browser Support

The numbers in the table indicate the first browser version that fully supports this method.

Method Chrome IE Firefox Safari Opera
console.timeEnd() Support 11 10 4 Support