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 »
Definition and Usage
The console.time() method serves as the starting method of the timer.
This method is generally used to test the execution time of a program.
console.timeEnd() method is the ending method of the timer and displays the execution time on the console.
If a page needs to use a timer in multiple places, you can add a label parameter to distinguish them.
Tip: During testing of 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 that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| console.time() | Yes | 11 | 10 | 4 | Yes |
Parameter Description
| Parameters | Type | Description |
|---|---|---|
| label | String | Optional. Used to set a label for the timer. |
| tablecolumns | Array | Optional. An array containing the names of the table header columns. |
More examples
Example
Use the label parameter:
console
.
time
(
"
Runoob
"
)
;
for
(
i
=
0
;
i
<
100000
;
i
++
)
{
//
Code section
}
console
.
timeEnd
(
"
Runoob
"
)
;
Try it »
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 »
Console object