views:

751

answers:

3

The Date object in javascript performs differently machine to machine and browser to browser in respect to the function's resolution in milliseconds. I've found most machines have a resolution of about 16 ms on IE, where Chrome or Firefox may have a resolution as good as 1ms.

Is there another function available to javascript in general or IE specifically that will give a better time resolution? I am trying to trap and record keyDown and keyUp times in milliseconds and need it in the +/- 10 ms range or less.

To see an illustration of this, check out the "resolutions of new date()" section of this page. There is a table with a test button that evaluates the current machine/browser's javascript time resolution in milliseconds. Interestingly, Chrome regularly gets a resolution of 1ms.

http://www.merlyn.demon.co.uk/js-dates.htm#OV

My quest is for a javascript date-time method that will give sub 10ms resolution across browsers. something to replace or improve Date().

A: 

AFAIK, milliseconds is as good as it gets in JavaScript. Here is the Mozilla.org documentation for the Date object. Nothing in there indicates anything with finer resolution.

Andrew Hedges
Agreed - I am finding with Internet Explorer that the best resolution I am seeing is 16ms ticks for the Date() object in javascript. Looking for something on IE with better resolution then that. 1ms resolution, consistant across browsers, would rock!
WillR
+1  A: 

Since you are mentioning Internet Explorer, I assume that you are working on Windows. The 15 ms resolution you are getting may have to do with the Windows system timer resolution.

I've also noticed through running Java programs on Windows, that the resolution of the system timer is around 16 ms or so. (Using the System.currentTimeMillis() method.)

I did a quite search to see if I could find any information on the system timer resolution in Windows, and was able to find a link to Inside Windows NT High Resolution Timers from TechNet. It mentioned a little bit about the resolution of the Windows system timer:

Windows NT bases all of its timer support off of one system clock interrupt, which by default runs at a 10 millisecond granularity. This is therefore the resolution of standard Windows timers.

(I'm assuming that Windows XP and Vista still has the same timer, consider it is a descendent of NT.)

Unless Firefox and Chrome have their own high-resolution timer implemented, I believe that the best resolution you'll be able to get from a browser on the Windows platform is going to be around 10 ms.

Although not relevant to this question, I also did find an article on MSDN on high-resolution timers on Windows: mplement a Continuously Updating, High-Resolution Time Provider for Windows

coobird
+2  A: 

High resolution timing is on a desktop machine is still an open topic.

Todays popular operating systems provide you only with a granularity of 10 ms, because that's the frequency of their clock timer interrupt. You will find the 10 ms also in Linux manpages, for example. The browser will only expose the timers provided by the operating system, with added browser-internal overhead.

That said, it is possible to achive a higher granularity. But all these techniques are specific to the hardware setup and you cannot expect them to be exposed through JavaScript in the near future.

ypnos
I don't know about the OS, but the browsers seem to keep a 10ms minimum, except for google chrome which has a 1 ms minimum: http://ejohn.org/blog/javascript-in-chrome/
enobrev
Reading that post it sounds like Chrome achieves this with busy wait.
ypnos