JavaScript

What Does console.time() Method Do in JavaScript

The execution time of the coding operation plays a significant role in all programming languages. It is generally used for “testing” purposes to identify how much time the code has taken to execute and for comparing the performance of the functionalities to analyze them. Like other programming languages, JavaScript offers the built-in “console.time()” method to analyze the execution time of a particular functionality.

.

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

console.time(label)

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:

<h2>console.time() Method in JavaScript</h2>

<p>Open the Console(F12) to Check the Execution Time </p>

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:

<script>

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:

<script>

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.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.