Measure execution time of a function using javascript

You can measure the performance of javascript code using console.time() function. This can be helpful in debugging and optimizing code which is taking lot of memory.


console.time() can be used with console.timeEnd().

console.timeEnd() function tells console to stop calculating the time.

var time = "";
 console.time("time"); //start the timer
 for( let i =0; i < 10; i++) {
     console.log("Running loop :" + i + " times");
 }
 console.timeEnd("time"); //stop the timer

You will see result as below:

Running loop :0 times
Running loop :1 times
Running loop :2 times
Running loop :3 times
Running loop :4 times
Running loop :5 times
Running loop :6 times
Running loop :7 times
Running loop :8 times
Running loop :9 times
time: 3.016ms

Please note console.time() and console.timeEnd() is supported by modern browsers only.