HTML4 (~15ms Timers)
HTML5 (4ms Timers)
Efficient Script Yielding W3C Specification
The W3C Efficient Script Yielding specification defines a means for site developers to yield control flow to the user agent before running script. Web developers may want to use this interface to both allow user agent events and display updates to happen before continuing script execution and to avoid long running script dialogs in certain user agents. Many web pages use the setTimeout method with a specified period of zero to attempt to do the same thing. However, setTimeout enforces a minimum delay, even with a specified period of zero, that isn't uniformly implemented across user agents. Removing this minimum delay from setTimeout runs the risk of causing existing webpages, that have come to rely on the minimum delay, to break by going into a seemingly hung state while also significantly increasing the power consumption of the browser.
This demo visually sorts 250 items using the Quick Sort algorithm once per callback, making one swap during each browser callback. The more browser callbacks per second the faster the items will be sorted. While this is not how you would efficiently implement a sorting algorithm it's a fun way to visualize the impact of callbacks per second. Try sorting using the HTML4 resolution of 15.6ms per callback, the HTML5 resolution of 4ms per callback, and the new setImmediate approach of immediate callbacks. Special thanks to Erik Kay and Mike Belshe from the Google Chrome engineering team for the quick sort test used to demonstrate the benefits of the new setImmediate API. Additional information can be found online including great articles from Nicholas Zakas.