Efficient Script Yielding ("setImmediate")

Web developers commonly use the setTimeout method to break apart long running JavaScript operations. This approach allows the browser to process outstanding work, such as layouts and paints, and then waits for the specified period of time before calling back into JavaScript. This can be a problem because browsers can make up to 250 callbacks per second if the setTimeout method has a specified period of zero. This can result in increased power consumption and decreased battery life because the CPU is unable to enter power-lowering sleep states.

The new setImmediate method can solve the problems of using setTimeout by addressing the core performance problem without negatively impacting power consumption. As a result, web developers can use the setImmediate method to break apart long running JavaScript operations and will receive a callback immediately after the browser has processed outstanding work.

Here's how to use this new API. If you are currently using the following code:

var handle = setTimeout(spellCheck, 0);

You can replace setTimeout with setImmediate like this:

var handle = setImmediate(spellCheck);

API Reference

setImmediate

Internet Explorer Test Drive demos

setImmediate API

IEBlog posts

W3C Web Performance: Continuing Performance Investments

Web Performance APIs Rapidly Become W3C Candidate Recommendations

The Year in Review: W3C Web Performance Working Group

Using PC Hardware more efficiently in HTML5: New Web Performance APIs, Part 1

Specification

Efficient Script Yielding: Section 4.2