🌐 Virtual DOM & Reconciliation
reactintermediate

🌐Virtual DOM & Reconciliation

Virtual DOM is a lightweight in-memory representation of the real DOM. Reconciliation diffs old and new trees to compute minimal real DOM updates.

Memory Trick

VDOM is a blueprint. Building from blueprint is cheap. Building the actual house is expensive. React minimizes actual construction.

Log in to track your mastery & progress

1️⃣ How It Works

  1. State/props change triggers re-render (component function runs)
  2. React creates a new Virtual DOM tree (plain JS objects)
  3. React diffs it with the previous tree (reconciliation)
  4. Calculates minimal set of changes
  5. Commits changes to the real DOM

Key insight: Components re-run but the real DOM is touched minimally.

2️⃣ Diffing Algorithm Rules

React makes two assumptions for O(n) diffing:

  1. Different element types → destroy old subtree, create new one
  2. Same element type → update changed attributes only
  3. Lists → use key props to identify elements
jsx
// Good — stable keys items.map(item => <Item key={item.id} {...item} />) // Bad — index as key (breaks reordering) items.map((item, i) => <Item key={i} {...item} />)

3️⃣ Senior Signal

"Virtual DOM optimizes DOM updates, but component functions still re-run on every render."

This is what separates senior answers:

  • VDOM doesn't prevent re-renders
  • It prevents unnecessary DOM mutations
  • React.memo, useMemo, useCallback prevent component re-execution

Alternatives to VDOM:

FrameworkApproach
SvelteCompile-time static analysis, no VDOM
Vue 3Fine-grained reactivity (Proxy-based)
AngularChange detection zones

Was this helpful?

Test your Knowledge

2 questions to cement what you just learned.