The Cost of Memoization
useMemo incurs two costs:
- Memory: Storing the cached value and the dependency array.
- Computation: Comparing the dependencies on every render.
Example: Premature Optimization
// ❌ SLOWER than standard JS
const value = useMemo(() => {
return a + b;
}, [a, b]);
JavaScript engines (V8) are incredibly fast at math. React's overhead to check prevDependencies !== nextDependencies takes longer than just adding a + b.
When to Avoid It
- Simple primitive transformations: Strings, numbers, booleans.
- Cheap array methods:
maporfilteron small arrays (< 100 items). - Components that render often: If dependencies change every render,
useMemodoes nothing but add overhead.
Sponsored Content
Google AdSense Placeholder
CONTENT SLOT