Why can replacing synchronous JavaScript with requestAnimationFrame callbacks sometimes increase INP rather than decrease it?
Answered 3-minute answer rooted in 2 pages
Because requestAnimationFrame() does not make work smaller or move it off the main thread; it schedules the work to run just before the browser renders the next frame. If an event handler’s heavy work is moved into a rAF callback unchanged, that work now runs in front of the very frame INP is waiting for. The interaction’s next paint is delayed by the same amount of work, plus the wait for the frame. INP improves when work is split and the browser is allowed to paint first, not when the same block is relocated.
Where rAF work lands in an interaction
web.dev’s INP article measures each interaction from the input until “the browser has painted the next frame”, in three phases: input delay, processing duration and presentation delay. Work scheduled with requestAnimationFrame() from a handler runs before the browser paints that next frame. So moving the work there does not take it out of the interaction:
| Version | What runs before the next paint | Effect on INP |
|---|---|---|
| Heavy work in the handler | Handler plus heavy work | Slow |
| Heavy work moved into a rAF callback | Handler, then heavy work in rAF, then rendering | About as slow, sometimes slower |
| Visible update in the handler, heavy work deferred past the paint | Handler's small update and rendering | Fast; the heavy work runs after the frame |
The second row can be slower when the move adds a wait for the next frame or when the rAF work triggers style and layout immediately before rendering.
The pattern web.dev recommends instead
The INP optimization guide uses requestAnimationFrame() differently: not to hold the heavy work, but as a marker for “after the next frame”. Its example queues the deferred work with setTimeout() from inside a requestAnimationFrame() callback. The rAF callback runs before the next paint, and the setTimeout() inside it schedules the heavy work as a new task that runs after that paint. The guide calls this “an effective method that works in all browsers to prevent non-critical code from blocking the next frame”.
The difference from the mistaken migration is the extra step. rAF alone puts the work before the paint; rAF plus setTimeout() puts it after.
Other ways rAF work can add to the frame
Layout thrashing. Code in a rAF callback that changes styles and then reads layout values, such as element sizes, forces the browser to calculate layout synchronously. The INP guide describes this forced synchronous layout as a rendering performance problem. Doing it right before rendering adds that work to the interaction’s frame.
Several callbacks stacked on one frame. If different components each schedule rAF work in response to the same interaction, all of it runs before the same paint.
How to fix a handler that was “fixed” with rAF
- Identify the visible change the visitor needs to see immediately, and do only that in the handler.
- Defer the rest past the paint, using the rAF plus
setTimeout()pattern or another way of yielding to the main thread. - Break large deferred work into chunks so later interactions are not blocked by it. web.dev’s long tasks guide treats any task over 50 milliseconds as long.
- Keep layout reads before writes, and avoid reading layout inside the same callback that just changed styles.
- Measure the interaction with INP attribution in the field and a trace in the lab. The trace should show a short task before the paint and the heavy work after it.
requestAnimationFrame() is the right tool for work that must happen before a frame, such as animation steps. Moving non-visual work there puts it exactly where INP measures it.
Answers that share these roots
- How should large single-page applications restructure event handlers to reduce INP without sacrificing interactivity on complex filtering interfaces? shares 2 pages
- Why do sites that strictly enforce a 200KB JavaScript budget sometimes still fail INP when they rely heavily on CSS animations and complex DOM trees? shares 2 pages
- What is the mechanism by which total JavaScript payload size affects INP even when the initial page load appears fast and LCP is within the good threshold? shares 2 pages
- How does Interaction to Next Paint differ mechanistically from First Input Delay in what it captures about main-thread responsiveness? shares 1 page
Leave a Reply