Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two fact you can exploit to measure the idleness of the javascript/flash thread.</p> <ol> <li><p>Javascript has timing sources (<a href="http://javascript.about.com/library/blstvsi.htm" rel="nofollow noreferrer">setTimeout, setInterval</a>) that tick only when javascript is idle. Thus the less idle the thread, the slower the timer.</p> <blockquote> <p>"JavaScript can only ever execute one piece of code at a time (due to its single-threaded nature) each of these blocks of code are "blocking" the progress of other asynchronous events. This means that when an asynchronous event occurs (like a mouse click, a timer firing, or an XMLHttpRequest completing) it gets queued up to be executed later." <a href="http://ejohn.org/blog/how-javascript-timers-work/" rel="nofollow noreferrer">How JavaScript Timers Work</a></p> </blockquote></li> <li><p>Javascript has an external timing source which ticks regardless of idleness. The class instance <a href="http://www.w3schools.com/jS/js_obj_date.asp" rel="nofollow noreferrer">Date()</a> is based on an external clock, you can get millisecond timing using <a href="http://www.w3schools.com/jsref/jsref_getTime.asp" rel="nofollow noreferrer">getTime()</a>:</p> <p>var timeSinceDisco = (new Date).getTime();</p></li> </ol> <p>eJohn has some interesting tests <a href="http://ejohn.org/blog/accuracy-of-javascript-time/" rel="nofollow noreferrer">using getTime() for benchmarking</a>.</p> <p>We use the difference between the external timer Date.getTime(), and the internal time setTimeout to calculate the degree of idleness of the javascript thread. Of course this isn't going to work as well on sexy new javascript engines like <a href="http://code.google.com/p/v8/" rel="nofollow noreferrer">V8</a> or <a href="https://wiki.mozilla.org/JavaScript:TraceMonkey" rel="nofollow noreferrer">tracemonkey</a> as they use more than one thread. It should work for most browsers currently out there.</p> <p><a href="http://ajaxian.com/archives/lessons-from-gmail-using-timers-effectively" rel="nofollow noreferrer">Lessons from Gmail: Using Timers Effectively</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload