Efficient Script Yielding with 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 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);

Replace setTimeout with setImmediate:

var handle = setImmediate(spellCheck);

API Reference



JavaScript Yielding: setImmediate API