If you need to get function execution time on your local development machine, you can either use your browser's profiling tools, or console commands such as console.time()
and console.timeEnd()
.
All modern browsers have JavaScript profilers built-in. These profilers should give the most accurate measurement as you do not have to modify your existing code, which could affect the function's execution time.
To profile your JavaScript:
- In Chrome, press F12 and select the Profiles tab, then Collect JavaScript CPU Profile.
- In Firefox, install/open Firebug, and click on the Profile button.
- In IE 9+, press F12, click on Script or Profiler (depending on your version of IE).
Alternatively, on your development machine, you can add instrumentation to your code with console.time()
and console.timeEnd()
. These functions, supported in Firefox11+, Chrome2+ and IE11+, report on timers that you start/stop via console.time()
. time()
takes a user-defined timer name as an argument, and timeEnd()
then reports on the execution time since the timer started:
function a() {
console.time("mytimer");
... do stuff ...
var dur = console.timeEnd("myTimer");
}
Note that only Firefox returns the elapsed time in the timeEnd()
call. The other browsers simply report the result to the developer console: the return value of timeEnd()
is undefined.
If you want to get function execution time in the wild, you will have to instrument your code. You have a couple options. You can simply save the start and end times by querying new Date().getTime()
:
function a() {
var start = new Date().getTime();
... do stuff ...
var end = new Date().getTime();
var dur = end - start;
}
However, the Date
object only has millisecond resolution and will be affected by any OS's system clock changes. In modern browsers, there's a better option.
The better option is to use the High Resolution Time, aka window.performance.now()
. now()
is better than the traditional Date.getTime()
in two important ways:
now()
is a double with submillisecond resolution that represents the number of milliseconds since the start of the page's navigation. It returns the number of microseconds in the fractional (e.g. a value of 1000.123 is 1 second and 123 microseconds).
now()
is monotonically increasing. This is important as Date.getTime()
can possibly jump forward or even backward on subsequent calls. Notably, if the OS's system time is updated (e.g. atomic clock synchronization), Date.getTime()
is also updated. now()
is guaranteed to always be monotonically increasing, so it is not affected by the OS's system time -- it will always be wall-clock time (assuming your wall clock is not atomic...).
now()
can be used in almost every place that new Date().getTime()
, + new Date
andt Date.now()
are. The exception is that Date
and now()
times don't mix, as Date
is based on unix-epoch (the number of milliseconds since 1970), while now()
is the number of milliseconds since your page navigation started (so it will be much smaller than Date
).
Here's an example of how to use now()
:
function a() {
var start = window.performance.now();
... do stuff ...
var end = window.performance.now();
var dur = end - start;
}
now()
is supported in Chrome stable, Firefox 15+, and IE10. There are also several polyfills available.
One other option for measuring execution time in the wild is UserTiming. UserTiming behaves similarly to console.time()
and console.timeEnd()
, but it utilizes the same High Resolution Timestamp that now()
uses (so you get a sub-millisecond monotonically increasing clock), and saves the timestamps and durations to the PerformanceTimeline.
UserTiming has the concepts of marks (timestamps) and measures (durations). You can define as many of either as you want, and they're exposed on the PerformanceTimeline.
To save a timestamp, you call mark(startMarkName)
. To get the duration since your first mark, you simply call measure(measurename, startMarkname)
. The duration is then saved in the PerformanceTimeline alongside your marks.
function a() {
window.performance.mark("start");
... do stuff ...
window.performance.measure("myfunctionduration", "start");
}
// duration is window.performance.getEntriesByName("myfunctionduration", "measure")[0];
UserTiming is available in IE10+ and Chrome25+. There is also a polyfill available (which I wrote).
performance.now()
does not work in Node.new Date().getTime()
will work in Node. – Ryan WalkerDate.now()
, it is also works in node – f278f1b2