WillR
WillR

Reputation: 69

In JavaScript, is there a source for time with a consistent resolution in milliseconds?

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().

Upvotes: 5

Views: 2311

Answers (3)

ypnos
ypnos

Reputation: 52417

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.

Upvotes: 2

coobird
coobird

Reputation: 161022

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

Upvotes: 2

Andrew Hedges
Andrew Hedges

Reputation: 21796

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.

Upvotes: 0

Related Questions