When Render Cost Exceeds Shallow Comparison Cost
What you'll learn: You'll understand when the performance benefit of skipping a render justifies the overhead of React.memo's shallow prop comparison.
The Cost-Benefit Equation
React.memo isn't free. Every time a parent re-renders, memo performs a shallow comparison of all props using Object.is. This comparison takes time—checking each prop key and comparing values. The critical question: does your component's render work cost more than this comparison check?
Think of it like a security checkpoint. If entering the building (rendering) takes 5 minutes but the security check (prop comparison) takes 30 seconds, the checkpoint saves you time when you don't need to enter. But if entering only takes 10 seconds, that 30-second check actually slows you down.
When the Math Works Out
React.memo pays off when your component's render work is expensive:
- Complex JSX trees with many nested elements
- Heavy calculations inside the render function
- Large lists of child components
- Expensive operations like mapping/filtering data
- Components that return hundreds of DOM nodes
The shallow comparison might check 3-5 props in microseconds, but if rendering creates 50 DOM elements, performs array transformations, or instantiates many child components, skipping that work is a clear win.
When It Doesn't
If your component just returns <div>{props.text}</div>, the render is nearly instant. The shallow comparison overhead might actually equal or exceed the render cost. You've added complexity for zero gain.
Key Takeaway: Only use React.memo when the component's render work is demonstrably more expensive than the shallow prop comparison—profile with DevTools to verify the actual cost before optimizing.