Using console.time and console.timeEnd

This blog focuses on two very basic JavaScript console methods. console.time and console.timeEnd built in methods can be very useful when put to use.

At times you would have definitely wanted to mesaure the time taken by a loop or some process. A normal strategy to do it would be to store the timestamp of the start and end of the loop and subtract them to get the time taken in milliseconds.

An efficient alternative to the above scenario would be to use console.time and console.timeEnd. Lets look at the code below which will the context more clear:

Example :

    console.time('Time-Taken');
    for(var i = 0; i < 10; i++){
        //Some code
    }
    console.timeEnd('Time-Taken');

Running the above code will yield this output: Time-Taken: 1.801ms

What console.time('Time-Taken') does is it starts a timer, whereas console.timeEnd('Time-Taken') stops the timer with name Time-Taken and outputs the elapsed time to the console.

We can use these methods in situations where we need to measure time between two events. I hope this post helps you in your daily cause. :)

comments powered by Disqus