Cooperative Scheduling with `requestIdleCallback`

Avatar of Chris Coyier
Chris Coyier on

Potch:

If you absolutely must perform tasks on the main thread (mutating the DOM or interacting with main-thread-only Web APIs), you can now request the browser provide you with a window of time in which it is safe to do so!

requestIdleCallback allows the browser to wait until it identifies an idle period. An idle period may be a few milliseconds between painting individual frames.

This seems like the kind of thing that becomes a best practice. But how do we identify which things we should use this for and what things we shouldn’t? Is it like “anytime we touch the DOM we should wrap it in this?” Perhaps with a twist of “unless you’re displaying new content to the user, which takes precedence over animation jank.” If you’re a framework that abstracts away DOM touching (and other main thread tasks), how do you decide to prioritize or not?

Direct Link →