JavaScript Console Time() Method

JavaScript Console Time() Method with Examples

In this article, I am going to discuss JavaScript Console Time() Method with Examples. Please read our previous article where we discussed JavaScript Console Table() Method with Examples.

JavaScript console.time() Method

The console.time() method is used to track the amount of time an operation or function takes. The console.time() method is used to start the timer that can be used to calculate the duration of an operation. The console.End() method is used to end/stop the timer and will print the total run time in milliseconds. The console.timeLog() method is used to print the current value of the timer that was previously started by calling console.time() method in the web console.

This method is used for testing purposes to check the performance of a piece of code. We can run the timer as many as we want at the same time. We can pass a label as the first argument of the console.time() method to describe what it is concerned about. But the label is optional in this method. but when we use the console.time() method multiple times in that case label is recommended.

In other words, this method is used to check how long something takes(operation/function). The console.time() and console.timeEnd() method is here to help us.

Syntax: console.time(label)

Parameters:
  1. label – This is an optional parameter. A label is given to a timer to start.
Return Value:

if no label parameter is provided in the method, it will return the output as

default: 2.286865234375 ms

otherwise, with the label, it will show the output as

timeout timer: 514.197021484375 ms

What we discussed above is given in the below example.

Example to Understand JavaScript Console Time() Method
<html>
<head>
    <title>JavaScript console time() method example</title>
</head>
<body>
    <script>
        //sample code1 with label
        console.time('sample code1');
        var student = {
            course: "JavaScript",
            price: "5000"
        };
        console.timeEnd('sample code1');

        //sample code2
        console.time();
        for (i = 0; i <= 5; i++) {
            let sum = i + 3;
        }
        console.timeEnd();

        //sample code3
        console.time("time start");
        console.log("click to continue");
        console.timeLog("time start");
        console.log("click again");
        console.timeEnd("time start");

        //sample code4
        console.time('function testing');
        let fun1 = function () {
            console.log('fun1 is running');
        }
        let fun2 = function () {
            console.log('fun2 is running..');
        }
        fun1();// calling fun();
        fun2(); // calling fun2();
        console.timeEnd('function testing');

    </script>
</body>
</html>

Output: Press F12 and go to the Console section

Example to Understand JavaScript Console Time() method

JavaScript console.timeEnd() Method

The console.timeEnd() is used to stop the timer that was previously started by calling the console.time() method. Once it is stopped the elapsed time is displayed to the web console automatically.

This method is used for testing purposes to check the performance of a piece of code. We can run the timer as many as we want at the same time. We can pass a label as the first argument of the console.timeEnd() method to describe what it is concerned about. But the label is optional in this method. But when we use the console.timeEnd () method for multiple times in that case label is recommended.

Syntax: console.timeEnd(label);

Parameters:
  1. label – This is an optional parameter. A label is given to a timer to stop. Once it is stopped the elapsed time is displayed to the web console automatically.
Return Value:

if no label parameter is provided in the method, it will return the output as

default: 2.286865234375 ms

otherwise, with the label, it will show the output as

timeout timer: 514.197021484375 ms

What we discussed above is given in the below example.

Example to Understand JavaScript Console TimeEnd() Method
<html>
<head>
    <title>JavaScript console timeEnd() method example</title>
</head>
<body>
    <script>
        //sample code1 with label
        console.time('sample code1');
        var student = {
            course: "JavaScript",
            price: "5000"
        };
        console.timeEnd('sample code1');

        //sample code2
        console.time();
        for (let i = 0; i < 100000; i++) {
            // some code
        }
        console.timeEnd();

        //sample code3
        console.time("timeout timer");
        setTimeout(() => {
            console.timeEnd("timeout timer")
        }, 1000);

    </script>
</body>
</html>

Output: Press F12 and go to the Console section

Example to Understand JavaScript Console TimeEnd() Method

JavaScript console.timeLog() Method

The console.timeLog() method is used to print the current value of the timer that was previously started by calling the console.time() method in the web console.

This method is used for testing purposes to check the performance of a piece of code. We can run the timer as many as we want at the same time. We can pass a label as the first argument of the console.time() method to describe what it is concerned about. But the label is optional in this method. but when we use the console.time() method multiple times in that case label is recommended.

Syntax: console.timeLog(label);

Parameters:
  1. label – This is an optional parameter. A label is given to a timer to stop. Once it is stopped the elapsed time is displayed to the web console automatically.
Return Value:

if no label parameter is provided in the method, it will return the output as

default: 2.286865234375 ms

Otherwise, with the label, it will show the output as

timeout timer: 514.197021484375 ms

If there is no timer running in the web console, the console.timeLog() method returns the warning

Timer ‘timeout timer’ does not exist.

What we discussed above is given in the below example.

Example to Understand JavaScript Console TimeLog() Method
<html>
<head>
    <title>JavaScript console timeLog() method example</title>
</head>
<body>
    <script>
        //sample code1
        console.time('sample code1');
        var student = {
            course: "JavaScript",
            price: "5000"
        };
        console.timeLog('sample code1');
        console.timeEnd('sample code1');

        //sample code2
        console.time("timer start");
        console.info("Click to continue");
        console.timeLog("timer start");
        console.info("click again");
        console.timeEnd("timer start");

        //sample code3
        console.time("timeout timer");
        setTimeout(() => {
            console.timeEnd("timeout timer")
        }, 1000);
        setTimeout(() => {
            console.timeLog("timeout timer")
        }, 500);
    </script>
</body>
</html>

Output: Press F12 and go to the Console section

In the next article, I am going to discuss JavaScript Console Trace() Method with Examples. Here, in this article, I try to explain JavaScript Console Time() Method with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *