Common Mistake: Memoizing Cheap Operations
What you'll learn: Why wrapping simple operations in useMemo often makes your code slower, not faster.
The Overhead Problem
Memoization isn't free. Every time you use useMemo, React must:
- Check if dependencies have changed (array comparison)
- Store the previous result in memory
- Retrieve and return the cached value
For truly cheap operations—simple math, string concatenation, or reading object properties—this overhead costs more than just recalculating the value.
What Counts as "Cheap"?
These operations are faster than memoization:
// ❌ Wasteful memoization
const fullName = useMemo(() => `${firstName} ${lastName}`, [firstName, lastName]);
const doubled = useMemo(() => count * 2, [count]);
const userEmail = useMemo(() => user.email, [user]);
// ✅ Just calculate directly
const fullName = `${firstName} ${lastName}`;
const doubled = count * 2;
const userEmail = user.email;
JavaScript engines execute these operations in microseconds. The dependency array comparison alone takes longer.
The Analogy
Imagine hiring a personal assistant to add 2 + 2 for you. By the time you explain the task, wait for their response, and verify their work, you could have done the math yourself dozens of times. That's useMemo on cheap operations.
When to Actually Use useMemo
Reserve memoization for operations that take noticeable time: filtering large arrays, transforming complex data structures, or computations with nested loops. If you can't measure a performance difference with the React DevTools Profiler, don't memoize it.
Key Takeaway: Only memoize operations that are genuinely expensive—simple calculations are faster to recompute than to memoize.