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.


Description

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:

The console.timeEnd() Method.


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 Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 11


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.