A JavaScript Console Excercise

Question: what is the approximate resolution of Mozilla’s JavaScript timing facilities as exposed by the global Date object?

First, there are two ways to get a millisecond representation of the current time in JS: Date.now() and new Date().getTime(). The first is faster because it doesn’t have the overhead of actually constructing a new object and then accessing a field; instead, it’s a single native code call on an existing object (that is, it’s a static method).

So, we can construct a simplistic microbenchmark, to be evaluated in the JavaScript Console, like so:

var r = []; for(var j = 25; j; --j) { var last = Date.now(); var i = 0; while(++i) { if(last != Date.now()) { break; } } r.push(i); } var t = 0; for(p in r) t += r[p]; t/r.length;

This takes the current timestamp and sees how many timestamps it can check before the returned value seems to change. It does this 25 times and gives the average of the values. A larger average value means more checks between last and Date.now(), and, presumably, a faster method of getting the current timestamp. Comparing last to Date.now() gives ~5000, while instantiating a new Date object and calling .getTime() gives values in the neighborhood of ~1750.

However, that’s really ALL the above code does. There’s a subtle semi-bug: we must keep in mind that the values returned are not necessarily going to increment cleanly. That is, it’s entirely possible that ( Date.now() - Date.now() ) could evaluate to something like 13, not just 0 or 1.

Thus, we can factor in the “jump” to the values we’re accumulating, like so:

var r = []; for(var j = 25; j; --j) { var last = Date.now(); var i = 0; while(++i) { if(last != Date.now()) { break; } } r.push(i / (Date.now() - last)); } var t = 0; for(p in r) t += r[p]; t/r.length;

This gives us a rough estimate of the number of timestamps it checks per millisecond. Using Date.now() gives me ~275, while new Date.getTime() is ~100.

Finally, accumulating only the ms deltas:

var r = []; for(var j = 25; j; --j) { var last = Date.now(); var i = 0; while(++i) { if(last != Date.now()) { break; } } r.push(Date.now() - last); } var t = 0; for(p in r) t += r[p]; t/r.length;

Here, both Date.now() and new Date.getTime() yeild values around 20, suggesting that Mozilla’s JS-accessible timestamps are going to be accurate to within about a fiftieth of a second.


*: Date.now() is a Mozilla-specific static function that isn’t in the ECMA-262 specification (better known as JavaScript, The Standard), and isn’t implemented by Internet Explorer. Useful for extension authors, not so much for anyone else.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s