Performance: Render Computation is Fast
What you'll learn: You'll understand why computing values during render is usually faster than using useEffect to keep state in sync.
The Speed of Modern JavaScript
A common fear when moving logic out of useEffect and into the render phase is: "Won't this slow down my component?" The surprising answer is almost always no.
Modern JavaScript engines are incredibly fast. Simple computations—filtering arrays, calculating totals, transforming data—execute in microseconds. Even operations that feel "expensive" often complete faster than a single frame (16ms).
The Hidden Cost of Effects
What is slow? The useEffect approach:
- Initial render with stale/empty state
- React commits to DOM
- Effect runs after paint
setStatetriggers- Second render
- React updates DOM again
That's two renders and two DOM updates instead of one. The "optimization" actually made things slower.
The Mental Model
Think of it like cooking: computing during render is like chopping vegetables while the pot heats up. Using useEffect is like heating the pot, then pausing everything to chop vegetables, then reheating the pot. The chopping itself is fast—it's the stopping and starting that wastes time.
// Fast: one render
const filtered = items.filter(item => item.active);
// Slower: two renders
const [filtered, setFiltered] = useState([]);
useEffect(() => {
setFiltered(items.filter(item => item.active));
}, [items]);
The first approach is simpler and faster.
Key Takeaway: Render computation is extremely fast in modern JavaScript. Computing derived values directly during render is almost always faster than synchronizing them with useEffect, which forces double rendering.