Dany Paredes
Danywalls | Angular ♥ Web


Danywalls | Angular ♥ Web

How to measure javascript code with performance.now()

Photo by Charles Deluvio on Unsplash

How to measure javascript code with performance.now()

How to calculate how much time take your code

Dany Paredes's photo
Dany Paredes
·Feb 11, 2021·

1 min read

Play this article

Sometimes, we want to know how is the performance or time spent in some function or process can be solved using performance.now().

performance.now() help us get time in milliseconds, and we can measure the time between some function completing his process.

Using performance.now

We can take the current performance.now() and compare with the same when our function finishes the process, as my example.

function getUsers() {
  let from = performance.now();
  setTimeout(() => {
    console.log("Getting users..")
    let to = performance.now()
    let total = from - to;
    console.log(`Total miliseconds ${total}`); 
// Total miliseconds -3003

If you want to learn more about it, please go free to read more:

Share this