4 ms·
Accuracy of JavaScript Time [2008]
- rix0r 16y agoI 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.
- izuzak 16y agoa more recent study of getTime() here: http://mivankovic.blogspot.com/2011/03/research-results-can-javascipt-gettime.html http://mivankovic.blogspot.com/2011/03/research-results-can-... ("Can JavaScipt getTime() be used for measuring performance of JavaScript code?")
- TimJYoung 16y agoWindows has a "GetSystemTime" resolution of 10-15 milliseconds, hence the results: http://msdn.microsoft.com/en-us/magazine/cc163996.aspx 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 16y agoI just ran a quick test on Win7, it's 1 ms.
- bmcleod 16y agotl;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.
- AshleysBrain 16y agoWhy don't browsers on Windows implement QueryPerformanceCounter (a microsecond precision timer)? In fact, I thought Firefox 4 had done this.
- pjscott 16y agoOn 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 http://getfirebug.com/wiki/index.php/Console_API https://github.com/eligrey/timer.js https://github.com/eligrey/timer.js
- jgrahamc 16y agoIt'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 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#L205 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#L180 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 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 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/src/platform-win32.cc&q=void%20Time::SetToCurrentTime%20package:http://v8%5C.googlecode%5C.com&sa=N&cd=1&ct=rc&l=446 http://www.google.com/codesearch/p?hl=en#W9JxUuHYyMg/trunk/s...
- broofa 16y agoWhy is this making the front page of hacker news? This article is 2.5 years old.
- franze 16y agoa) it's still an awesome article b) it's probably time to revisit that topic