Time Slicing: Yielding Control Back to the Browser
What you'll learn: How React breaks rendering work into small chunks and periodically pauses to keep the UI responsive.
The Core Problem
JavaScript is single-threaded. When React is busy rendering a complex component tree, the browser can't respond to user input, scroll events, or even update animations. The UI freezes until rendering completes.
How Time Slicing Works
Time slicing is React's solution: instead of rendering everything in one continuous block, React breaks the work into small chunks (typically 5ms each). After each chunk, React yields control back to the browser's main thread.
This pause lets the browser:
- Process pending user interactions (clicks, typing)
- Handle scroll events
- Run animations
- Check if higher-priority work has arrived
If something urgent comes in (like user input), React can interrupt the current low-priority render, handle the urgent work, then resume the original task later.
The Kitchen Analogy
Imagine chopping vegetables for a big salad. Without time slicing, you'd chop everything before checking if the oven timer went off. With time slicing, you chop for 10 seconds, glance at the stove, chop for another 10 seconds, check again. The salad takes slightly longer total, but you never miss the oven timer.
Why It Matters
Time slicing is what makes transitions and deferred values feel responsive. When you mark an update as low-priority (startTransition or useDeferredValue), React uses time slicing to render it. The app stays interactive because React regularly checks for urgent work during the render.
Key Takeaway: Time slicing breaks React's rendering work into interruptible chunks, periodically yielding to the browser to maintain responsiveness during low-priority updates.