HTML DOM console.time()Method
Example
The actual time required to execute 100,000 times:
console.time();
for (i = 0; i < 100000; i++) {
//Code section
}
console.timeEnd();
Try it yourself »
Definition and Usage
The console.time() method is the starting method of the calculator.
This method is generally used to test the duration of program execution.
console.timeEnd()The method is the ending method of the calculator, and displays the execution duration on the console.
If multiple places on a page need to use the calculator, you can add a label parameter to set it.
Tip:When testing this method, the console needs to be visible (press F12 in the browser to open the console).
Syntax
console.time(label)
Browser Support
The numbers in the table indicate the first browser version number that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| console.time() | Yes | 11 | 10 | 4 | Yes |
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| label | String | Optional, used to set a label for the calculator. |
| tablecolumns | Array | Optional, an array, the names of the table column headers. |
More Examples
Example
Using the label parameter:
console.time("Example");
for (i = 0; i < 100000; i++) {
//Code section
}
console.timeEnd("Example");
Try it yourself »
Example
Test which code executes faster
var i;
console.time("for loop test");
for (i = 0; i < 100000; i++) {
//Code section
}
console.timeEnd("for loop test");
i = 0;
console.time("while loop test");
while (i < 1000000) {
i++
}
console.timeEnd("while loop test");
Try it yourself »
Console Object