Performance: Functional Updates Are Not Slower
What you'll learn: Why using the functional update form setState(prev => ...) doesn't hurt performance.
The Myth
Some developers worry that passing a function to setState must be slower than passing a value directly. After all, doesn't React have to call that function? Won't that add overhead?
The Reality
React calls your updater function exactly once per state update. There's no performance penalty.
Here's what actually happens:
// Direct update
setCount(count + 1);
// React: "Set count to this value"
// Functional update
setCount(prev => prev + 1);
// React: "Call this function once with current state, use the result"
In both cases, React performs one update. The functional form doesn't get called multiple times unless you call setCount multiple times.
When Batching Happens
React batches multiple setState calls in the same event handler, but that doesn't mean your function runs multiple times:
function handleClick() {
setCount(prev => prev + 1); // Function called once
setCount(prev => prev + 1); // Function called once
// React batches both, applies them sequentially
}
Each updater runs exactly once. React queues them and applies them in order during the single re-render.
Think of It Like This
It's like giving instructions to an assistant:
- Direct: "Set the temperature to 72°F"
- Functional: "Increase the current temperature by 5°"
Both instructions take the same amount of effort to execute. The functional form just reads the current value first—something React does instantly.
Key Takeaway: Functional updates have zero performance overhead; they're just a safer way to handle updates that depend on previous state.