React Curriculum

The Reconciliation Diffing Algorithm & Heuristics

Medium100% Free~25 mins#react#reconciliation#diffing#heuristics#vdom#keys
Interview Answer (30-Second Summary)

React uses an O(n) heuristic diffing algorithm based on two assumptions: two elements of different types produce completely different trees, and the developer can hint which child elements are stable across renders with a key prop.

Key Learning Objectives
✓

Explain why general tree comparison algorithms are O(n^3) and how React achieves O(n).

✓

Analyze the element type heuristic: changing <div> to <span> destroys and remounts the entire subtree.

✓

Analyze the same-type heuristic: preserving DOM nodes and only updating changed attributes.

✓

Predict whether component state is preserved or wiped out across render passes.

The Interview Problem & Context

What is logged to the console when comparing state preservation between different element types, same element types with different keys, and identical element types?

1// Simulating React tree comparison heuristics
2function simulateReconciliation() {
3 const results: string[] = [];
4
5 // 1. Different element types: <div> -> <section> destroys & remounts
6 const changeType = 'remount';
7 results.push(changeType);
8
9 // 2. Same element type, same position, no key change: preserves state!
10 const sameTypeSameKey = 'keep-state';
11 results.push(sameTypeSameKey);
12
13 // 3. Same element type, attribute changed (className): recycles DOM node
14 const attrChange = 'recycle';
15 results.push(attrChange);
16
17 return results.join(' ');
18}
19
20console.log(simulateReconciliation());
Predict Console & Render Output
A

remount keep-state recycle

B

recycle keep-state remount

C

remount remount recycle

D

keep-state recycle remount

Execution Trace
Step 1 of 4 (Line 6)

Different element types tear down old Fiber tree and mount fresh tree.

Component / Call Stack
simulateReconciliation()
Hook State & Scope
results[0]:"remount"
Console Output(No console logs emitted)
Deep Architectural Explanation

The Reconciliation Diffing Heuristics

Finding the minimum number of operations to transform one arbitrary tree into another has a known complexity of $O(n^3)$ (Levenshtein distance for trees). For 1,000 elements, that is 1 billion operations per frame!

React implements a practical $O(n)$ heuristic algorithm based on two core assumptions:

1. Elements of Different Types Produce Different Trees

Whenever the root elements have different types:

tsx
// Before:
<div><Counter /></div>
// After:
<span><Counter /></span>
  • React does not attempt to match the <Counter /> instances.
  • It destroys the entire <div> subtree (unmounting all children and wiping their state) and builds the <span> tree from scratch.

2. Same Element Types Are Mutated in Place

When comparing two React DOM elements of the same type:

tsx
// Before:
<div className="before" title="stuff" />
// After:
<div className="after" title="stuff" />
  • React looks at the attributes of both, keeps the same underlying DOM node, and only mutates className!

3. Keys Guide Child Element Reordering

Without keys, inserting an item at the beginning of a list forces React to mutate every child. With keys, React uses a map lookup to match existing Fibers and simply moves the DOM nodes.

Common Interview Traps & Pitfalls
✕

Believing React performs an exhaustive mathematical diff of every tree node.

✕

Changing root element tags (div -> section) and wondering why child component state vanished.

✕

Using unstable keys (Math.random()) which forces React to treat every render as a full unmount and remount.

Senior Interviewer Follow-Up Probes
?

Why does React compare elements level-by-level (breadth-first) rather than depth-first?

?

How does the new React Compiler affect reconciliation diffing performance?

?

What happens when two components have the same type but different positions in the tree?

Frequently Asked Interview Questions
What is the time complexity of React's reconciliation algorithm?

O(n), where n is the number of elements in the tree, achieved through heuristic assumptions rather than general O(n^3) tree diffing.

React destroys the entire old DOM node and all its children, running unmount cleanups and mounting a completely new tree with reset state.

It preserves the underlying DOM node and only updates the specific attributes, styles, or event listeners that changed between render passes.

React's heuristic algorithm only diffs children at the same level of the tree. If a child moves to a different parent, React remounts it rather than tracking the move.

Keys provide stable identity across renders, allowing React to match children in lists even when they are inserted, deleted, or reordered.