HTML DOM console.time() Method
Example
How long does it take to perform a for-loop 100.000 times:
console.time();
for (i = 0; i < 100000; i++) {
// some code
}
console.timeEnd();
Try it Yourself »
Definition and Usage
The console.time() method starts a timer in the console view.
This method allows you to time certain operations in your code for testing purposes.
Use the console.timeEnd() method to end the timer and display the result in the console.view.
Use the label parameter to name the timer, then you are able to have many timers on the same page.
Tip: When testing console methods, be sure to have the console view visible (press F12 to view the console).
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
console.time() | Yes | 11 | 10 | 4 | Yes |
Syntax
console.time(label)
Parameter Values
Parameter | Type | Description |
---|---|---|
label | String | Optional. Use the label parameter to give the timer a name |
More Examples
Example
Using the label parameter:
console.time("test1");
for (i = 0; i < 100000; i++) {
// some code
}
console.timeEnd("test1");
Try it Yourself »
Example
Which is fastest, the for loop or the 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 »