In frameworks like React, where performance hinges on detecting whether props or state have changed. That is a reason why equality checks are crucial in software development because they determine when to re-render components, update state, or skip unnecessary computations.
If equality checks are too strict or too loose, it can lead to:
- Unnecessary re-renders
- Missed updates
So in this blog, let's deep dive Shallow equality and Deep equality in Javascript. Let's start!!!
1. What is Shallow Equality?
- Often use in React (React.memo, PureComponent)
+ React.memo: prevents unnecessary re-renders by shallow comparing props
+ PureComponent: uses shallow equality for props and state in shouldComponentUpdate
2. What is Deep Equality?
3. Shallow equality vs Deep equality
- Shallow:
+ Accuracy: Only check top-level
+ Use cases: React.memo, PureComponent, prop diffing,...
- Deep:
0 Comments