

Accuracy of JavaScript Time [2008] - franze
http://ejohn.org/blog/accuracy-of-javascript-time/

======
jgrahamc
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#...](http://source.winehq.org/source/dlls/kernel32/kernel_main.c#L205),
it calls GetTickCount64 which is here
[http://source.winehq.org/source/dlls/kernel32/kernel_main.c#...](http://source.winehq.org/source/dlls/kernel32/kernel_main.c#L180)
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...](http://www.google.com/codesearch/p?hl=en#W9JxUuHYyMg/trunk/src/platform-
win32.cc&q=void%20Time::SetToCurrentTime%20package:http://v8%5C.googlecode%5C.com&sa=N&cd=1&ct=rc&l=446)

------
TimJYoung
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.

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

------
bmcleod
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.

------
rix0r
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.

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

~~~
pjscott
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>

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

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

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

