Hacker News new | past | comments | ask | show | jobs | submit login
Accuracy of JavaScript Time [2008] (ejohn.org)
35 points by franze on April 5, 2011 | hide | past | favorite | 10 comments



It's such a pity that he doesn't dig into why this is. He makes this funny statement:

  Internet Explorer 8 (I also tested 6, for good measure, 
  with the same results), Opera, Safari, and WebKit Nightly   
  all bin their results. There is no 'normal distribution' 
  whatsoever. Effectively these browsers are only updating 
  their internal getTime representations every 15 
  milliseconds.
He can't really think that all those browsers have implemented a binning algorithm. Underlying this is the GetTickCount (http://msdn.microsoft.com/en-us/library/ms724408.aspx) Windows API function: "The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds." That goes all the way down into the bowels of the PC where there's an interrupt occurring that is used to update the tick count. Depending on the machine/operating system that you are using the system timer updates at varying frequencies (the original IBM PC had an 18.2Hz timer giving updates about every 54ms).

Related to this is how Windows' GetSystemTime is implemented and how often it updates.

He goes on to say:

  It's pretty obvious that the Wine layer is tapping into some
  higher-quality timer mechanism and is providing it to IE - giving
  us a result that is even more accurate than what the browser normally
  provides
Use the source, John! Wine is calling gettimeofday(). GetTickCount is defined here: http://source.winehq.org/source/dlls/kernel32/kernel_main.c#..., it calls GetTickCount64 which is here http://source.winehq.org/source/dlls/kernel32/kernel_main.c#... which calls NtQuerySystemTime which is here http://source.winehq.org/source/dlls/ntdll/time.c#L447 which calls gettimeofday.

Wine's GetSystemTime implementation is going the same thing: http://source.winehq.org/source/dlls/kernel32/time.c#L933

In Chrome this is quite well documented, see comments here for Windows: http://www.google.com/codesearch/p?hl=en#W9JxUuHYyMg/trunk/s...


Windows has a "GetSystemTime" resolution of 10-15 milliseconds, hence the results:

http://msdn.microsoft.com/en-us/magazine/cc163996.aspx

Some comments found via Google indicate that this has changed to 1ms in Vista and higher, but I can't confirm this.


I just ran a quick test on Win7, it's 1 ms.


tl;dr getTime in windows only updates every 15 ms, so if your performance benchmark cares about that kind of period then it isn't going to give very good results.

Every time I hear about performance testing in JS it seems to be that it doesn't really handle IE very well for whatever reason it is today. It looks like this bug would have made IE look like it performs better than it actually does though.


I thought it was pretty well-known that standard system timers (except the High Performance ones) could/would be inaccurate. I'm actually surprised by how reliable the OS X timers are.

Normally, if you do any kind of benchmarking, you would run the test N times and average the times out.


Why don't browsers on Windows implement QueryPerformanceCounter (a microsecond precision timer)? In fact, I thought Firefox 4 had done this.


On Firefox, the console.time and console.profile functions provided by Firebug do this. On Chrome, you can use the chrome.interval object for the same purpose.

http://getfirebug.com/wiki/index.php/Console_API

https://github.com/eligrey/timer.js


a more recent study of getTime() here: http://mivankovic.blogspot.com/2011/03/research-results-can-... ("Can JavaScipt getTime() be used for measuring performance of JavaScript code?")


Why is this making the front page of hacker news? This article is 2.5 years old.


a) it's still an awesome article b) it's probably time to revisit that topic




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: