The Cost-Benefit Analysis of useMemo
What You'll Learn
You'll learn how to decide whether useMemo is worth using by weighing its overhead against the computation cost it saves.
The Hidden Cost of Optimization
useMemo isn't magic—it has real overhead. Every time you use it, React must:
- Store the memoized value in memory
- Compare every dependency in the array (using
Object.is) - Decide whether to return the cached value or recompute
Think of it like hiring a filing assistant. If finding a document takes 2 seconds and the assistant takes 3 seconds to check their files plus file new copies, you're actually slower with "help."
The Break-Even Point
Use useMemo only when:
Computation Cost > (Memoization Overhead + Comparison Cost)
For simple operations like arithmetic or string concatenation, the overhead exceeds the benefit:
// ❌ Don't do this - overhead costs more than the calculation
const doubled = useMemo(() => count * 2, [count]);
// ✅ Just calculate it - it's cheap
const doubled = count * 2;
But for expensive operations like filtering thousands of items or complex transformations, memoization wins:
// ✅ Good - filtering/mapping large arrays is expensive
const filteredUsers = useMemo(() =>
users.filter(u => u.active).map(u => ({ ...u, formatted: formatUser(u) })),
[users]
);
Making the Call
The overhead is justified when the computation is genuinely slow (milliseconds, not microseconds) or when preventing re-renders in child components matters. Always profile with React DevTools first—premature optimization wastes time and clutters code.
Key Takeaway: Only use useMemo when the cost of recalculating exceeds the cost of memoization itself plus dependency comparison—profile before optimizing.