DEV Community

loading...
Cover image for How to measure the time javascript function with performance.now()

How to measure the time javascript function with performance.now()

danywalls profile image Dany Paredes ・1 min read

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

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

Using performance.now

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

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

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

Discussion (0)

pic
Editor guide