0 points

Calculating execution time in Javascript


dashboard

Sun Jun 07 2020 03:10:31 GMT+0000 (UTC)

Posted by @biranchi125 #javascript

var t0 = performance.now();

for (let i = 0; i < 10000; i++) {   
    // Do stuff here 
}  

// Do some other stuff here

var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " ms.")
content_copy Copy

Sometimes we need to calculate the time taken by a piece of code to complete execution. The performance.now() can be used to compute the execution time.

https://levelup.gitconnected.com/5-javascript-tricks-that-are-good-to-know-78045dea6678