HTML DOM console.time() method

Definition and usage

console.time() method starts the timer in the console view.

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

Please use console.timeEnd() method End the timer and display the result in console.view.

Use the label parameter to name the timer, and then you can set multiple timers on the same page.

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

Using the 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 < 1000000; 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.time(label)

Parameter value

Parameter Type Description
label String Optional. Use the label parameter to name the timer.

Browser support

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

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