Q24: How does React diffing algorithm achieve O(n) complexity?

React reduces standard $O(n^3)$ tree diffing to $O(n)$ by applying two heuristics: two elements of different types generate different trees, and lists of child elements are tracked using developer-provided key props.

Key Takeaways
  • Heuristic assumptions optimize diffing from cubic $O(n^3)$ to linear $O(n)$ time complexity.

  • Changing element types unmounts the old subtree completely and builds a new one.

Knowledge Check

What happens in React diffing when an element changes its tag type from <div> to <section>?