Reputation:
Can anyone give advice as to the
accuracy and precision of timing in JavaScript?
I need to time user responses with a calculated accuracy and precision.
Date().getTime
was the function I was thinking about using to make measurements. I imagine the accuracy is near perfect, but I want to calculate the precision.
I want to emulate a user using setTimeout() so I can calculate precision of getTime().
setTimeOut()
Do these two functions use the same clock? If one varies say +10msec will the other as well effectively making my experiment to calculate precision useless?
Someone already did this:
http://ejohn.org/blog/accuracy-of-javascript-time/
Resolution Information
https://bugzilla.mozilla.org/show_bug.cgi?id=363258
Upvotes: 2
Views: 186
Reputation: 3502
You should read this article. It talks about the new High resolution Timer and compares it with the current Date
object.
Date.now() // 1337376068250 performance.now() // 20303.427000007
Basically there are two key points:
performance.now() is a measurement of floating point milliseconds since that particular page started to load
Perhaps less often considered is that Date, based on system time, isn't ideal for real user monitoring either. Most systems run a daemon which regularly synchronizes the time. It is common for the clock to be tweaked a few milliseconds every 15-20 minutes. At that rate about 1% of 10 second intervals measured would be inaccurate.
With Date
you'll get miliseconds resolution, though the accuracy of it is not guaranteed. According to the High resolution Timer specs:
For certain tasks this definition of time may not be sufficient as it does not allow for sub-millisecond resolution and is subject to system clock skew.
I'd recommend you read both articles to get a clearer idea about this.
Upvotes: 1