Module P-16·14 min read

How React's Virtual DOM is a tree, and how its diffing algorithm is DFS with a few production-grade shortcuts.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module P-16 — Capstone: DSA in Production — React's Virtual DOM, Diffing, and Tree Traversal

What this module covers: Practitioner closes by pointing Module P-4's tree traversal directly at React — arguably the single most-used piece of tree-shaped software most engineers touch daily. The Virtual DOM isn't a separate concept to memorize; it's a tree, compared against another tree, using traversal patterns and pruning heuristics that are direct descendants of everything covered since Module P-4.


The DOM Is a Tree — and Touching It Is Expensive

The browser's DOM (Module P-4's real-world anchor) is a tree of nodes. Every time you change it — adding an element, updating text, toggling a class — the browser potentially has to recalculate layout (reflow) and redraw pixels (repaint), both of which are comparatively expensive operations. Update the DOM directly, one small change at a time, dozens of times per render cycle, and you pay that cost dozens of times over.

React's answer: don't touch the real DOM directly for every small change. Instead, keep a lightweight, in-memory tree — the Virtual DOM — that mirrors what the UI should look like, compute the minimal set of real changes needed, and apply them to the actual DOM in one batched pass.


Reconciliation Is Tree Traversal

When a component's state changes, React builds a new Virtual DOM tree describing the updated UI, and compares it against the previous tree to figure out exactly what changed — this comparison process is called reconciliation, and it is, structurally, a tree traversal following the same depth-first, visit-node-then-children order as Module P-4's preorder traversal:

typescript

Process the current node first (compare type and props), then recurse into children — this is Module P-4's preorder traversal, applied to comparing two trees simultaneously instead of visiting one. The "collect patches, then apply them all together" step afterward is exactly the batching that avoids the expensive one-change-at-a-time DOM updates described above.


The Shortcuts That Make This Fast

A fully general "find the minimum edit distance between two trees" algorithm (a real, studied problem, structurally related to Module P-14's edit distance between two strings, generalized to tree structures) is far too slow to run on every keystroke — without simplifying assumptions, comparing two trees optimally is a much more expensive problem than comparing two flat sequences. React's actual speed comes from two deliberate heuristics that trade a small amount of theoretical optimality for a massive, practical speedup:

1. Different element types mean "don't bother diffing deeper — replace the whole subtree." If a <div> becomes a <span> at the same tree position, React doesn't try to figure out which of the div's children might correspond to which of the span's children — it assumes the entire subtree is different and rebuilds it from scratch. This sidesteps an expensive subtree-matching problem entirely, at the cost of occasionally doing more work than a perfectly optimal (but far slower) algorithm would.

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.