Algorithms and Data Structures · Senior · Question 35 of 75
Why does React's reconciliation run in O(n) when a general tree diff is about O(n³)?
- 1It hashes the whole tree and compares the hashes
- ✓Correct answer: It uses element types and keys as heuristics
- 3It compares only the root elements of both trees
Explanation
The minimal tree edit distance is expensive. React compares level by level: a changed type replaces the subtree, and key matches children between renders. These heuristics are almost always right for UI code.