Julius A Julius A - 1 year ago 93
Javascript Question

How to measure time taken by a function to execute

I need to get execution time in milliseconds.

I originally asked this question back in 2008. The accepted answer
then was to use new Date().getTime() However, we can all agree now
that using the standard performance.now() API is more
appropriate. I am therefore changing the accepted answer to this one.

Answer Source

You can use console.time: (non-standard)


someFunction(); // run whatever needs to be timed in between the statements


The string being pass to the time() and timeEnd() methods must match
(for the timer to finish as expected).


You could use the standard performance.now() API, like so:

var t0 = performance.now();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download