The Performance Cost of flushSync
What you'll learn: Why flushSync undermines React's performance optimizations and when that trade-off is acceptable.
The Optimization You're Sacrificing
Remember that React's batching system is a performance optimization. By grouping multiple state updates into a single render-commit cycle, React minimizes expensive DOM operations and browser repaints.
When you use flushSync, you're explicitly telling React: "Stop batching. Render and commit to the DOM right now, even if more updates are coming." This forces synchronous work that blocks the main thread.
Why This Hurts Performance
Think of batching like writing a grocery list before going to the store (one trip, many items). Using flushSync is like making a separate trip for each item — technically it works, but it's inefficient.
Concrete costs:
- Multiple render passes instead of one batched pass
- Multiple DOM commits trigger additional browser layout/paint cycles
- Blocked main thread means the UI can't respond to user input during the flush
- Lost optimization opportunities for sibling components that could've updated together
If you call flushSync multiple times in quick succession, each call forces its own complete render-commit cycle. In a complex component tree, this can mean milliseconds of wasted work multiplied by the number of calls.
When the Cost is Worth It
The trade-off makes sense only when you must have the DOM updated immediately for correctness — like measuring a newly rendered element's dimensions or synchronizing with a third-party library that reads from the DOM.
For 99% of cases, React's automatic batching is both faster and correct.
Key Takeaway: flushSync trades React's batching performance gains for immediate DOM updates — use it only when synchronous DOM access is absolutely necessary, never as a default pattern.