.
This write-up explains the working and usage of the “console.time()” method in JavaScript.
What Does the “console.time()” Method Do in JavaScript?
The “console.time()” method tracks the operation or function’s execution time. It starts the timer that calculates the duration of the specified JavaScript operation and ends up using the “console.timeEnd()” method.
Syntax
The “console.time()” supports an optional “label” argument that specifies the operation name. It is recommended in multiple occurrences of the “console.time()” method for the identification of each operation easily.
HTML Code
Go through the following HTML code:
In the above code snippet:
- The “<h2>” tag creates a subheading displaying the specified statement.
- The “<p>” tag specifies a paragraph.
Note: The above HTML code will be followed throughout the article.
Example 1: Applying the “console.time()” Method to Analyze the Execution Time of a Code Functionality (“for” Loop)
In this example, the “console.time()” method is used to get the execution time of the defined “for” loop in JavaScript.
JavaScript Code
Consider the below-stated lines of code:
console.time("for loop");
for (var a = 0; a < 1000; a++) {
}
console.timeEnd("for loop");
</script>
In the above code block:
- Specify the “console.time()” method having a label “for loop” at the start of the defined “for” loop.
- After that a “for” loop is initialized that will iterate 1000 times.
- Lastly, the “console.timeEnd()” method is defined that stops the timer and show the total run time of the “for” loop.
Output
As seen, the console shows the total execution time of the “for” loop in milliseconds (ms).
Example 2: Applying the “console.timeEnd()” Method to Compare the Execution Time of the Loops
This example explains how the “console.time()” method can be used to compare multiple operations i.e., “loops” execution time for comparison.
JavaScript Code
Overview of the following JavaScript code:
console.time("First for loop");
for (var i=0; i<1000; i++) {
}
console.timeEnd("First for loop");
console.time("Second for loop");
for (var a=0; a<2000; a++) {
}
console.timeEnd("Second for loop");
</script>
In the above code:
- First, specify the “console.time()” method to start the timer for the first initialized “for” loop.
- In the next step, the first “for” loop is initialized.
- After that, attach the “console.timeEnd()” method to stop the timer for getting the total execution time.
- Next, the “console.time()” method is applied again for the latter loop, and the “console.timeEnd()” method likewise stops the timer.
Output
As analyzed, the console shows the total run time of both the initialized “for” loops, thereby comparing them.
Conclusion
JavaScript offers the built-in “console.time()” method that starts the timer and calculates the duration of the specified code functionality. It helps in comparing the execution time of operations and for testing purposes. Once the timer starts, it stops with the help of the “console.timeEnd()” method.
This guide described the working and usage of the “console.time()” method in JavaScript.