How to measure a function execution time in JavaScript

1 Answer

0 votes
function mul(a, n) {
    let total = 0; 
    
    for (let i = 0; i < n; i++)
        total += a;
    
    return total;
}

// performance.now() method returns a high-resolution timestamp in milliseconds

const start = performance.now();

mul(10, 3000); 

const end = performance.now(); 

console.log('Function execute time: ' + (end - start) + ' milliseconds');



/*
run:

Function execute time: 0.12034999951720238 milliseconds

*/

 



answered May 24, 2021 by avibootz
edited Aug 5, 2024 by avibootz
...