Most developers use React every day without truly understanding what happens between
setState()and the moment your screen updates. This article pulls back the curtain.
The Problem: Why Direct DOM Manipulation Is Slow
Before React existed, developers manipulated the browser's DOM directly using JavaScript. Sounds simple enough — but there's a hidden cost that most beginners don't realize.
Every time you touch the real DOM, the browser has to do a lot of expensive work:
Recalculate styles across potentially hundreds of elements
Reflow/layout — figure out where every element should be positioned on the page
Repaint — actually draw the pixels on screen
Now imagine a dynamic UI where things are constantly changing — a live search bar, a real-time feed, a form with instant validation. If you're touching the DOM dozens of times per second with raw JavaScript, the browser is constantly re-doing all that expensive work. The result? Sluggish, janky interfaces.
The real DOM is not slow to read. It's slow to update frequently. That distinction matters a lot.
What Is the Virtual DOM?
The Virtual DOM is React's solution to this problem. At its core, it's a lightweight JavaScript object that mirrors the structure of the real DOM tree.
Think of it like a blueprint of your UI. Instead of updating the actual house every time you want to move a piece of furniture, you first update the blueprint, figure out exactly what needs to change, and then make the minimal set of real changes.
A Virtual DOM node for a simple <div> might look something like this:
js
{
type: 'div',
props: {
className: 'card',
children: [
{
type: 'h2',
props: { children: 'Hello, World!' }
}
]
}
}
It's just a plain JavaScript object. Creating and comparing these objects is extremely fast — no browser layout or painting involved at all.
Step 1: The Initial Render
When your React app loads for the first time, here's what happens:
React takes your JSX (which is just syntactic sugar for
React.createElement()calls) and builds a Virtual DOM tree in memory.React then looks at this Virtual DOM tree and uses it to create the actual HTML elements in the real DOM.
The browser takes those real DOM nodes and paints them on screen.
This initial render is a full construction — React has nothing to compare against yet, so it simply builds everything from scratch. The Virtual DOM is created, then translated to the real DOM, and that's your app's first frame.
Step 2: State or Props Change
Now imagine a user clicks a button and you call setState(), or a parent component passes new props down.
React doesn't immediately touch the real DOM. Instead, it does something smarter:
It creates a brand new Virtual DOM tree that reflects what the UI should look like after the change.
It holds onto the previous Virtual DOM tree — the one from before the change.
This is the key insight: React always has two Virtual DOM snapshots — the current one and the new one. The real work happens next.
Step 3: Diffing — The Reconciliation Algorithm
This is where React's intelligence really shines. Reconciliation is the process of comparing the old Virtual DOM tree with the new one to figure out exactly what changed.
React calls this process diffing.
Here's a simple mental model of how it works:
React compares trees node by node, top to bottom.
If two nodes have a different type (e.g., a
<div>changed to a<span>), React tears out the old subtree entirely and builds a fresh one.If two nodes have the same type, React looks at their props and children to see what changed. Only the changed attributes get updated.
For lists of elements, React uses the
keyprop you provide to match old items with new ones. This is why React warns you when you forget to add keys — without them, React can't efficiently figure out which list item moved, got added, or got removed.
React doesn't do a naive comparison of every possible combination of nodes — that would be an O(n³) algorithm, which would be catastrophically slow for large trees. Instead, React uses a set of heuristics that makes the diffing O(n) — linear time. It assumes:
Elements of different types produce completely different trees.
Sibling elements with stable
keyprops can be reliably matched across renders.
These are smart, practical assumptions that hold true in almost every real-world React app.
Step 4: Calculating the Minimal Update
After diffing is complete, React has a precise list of changes — the diff patch. This might be something like:
"Update the
textContentof this<p>element""Add this new
<li>to this<ul>""Remove this
<div>entirely"
Notice what React does not do: it does not re-render the whole page. It does not even re-render the parent container. It surgically applies only the changes that are actually needed.
This minimal set of real DOM mutations is the whole point of the Virtual DOM approach.
Step 5: Committing to the Real DOM
Once the diff patch is ready, React enters what's internally called the commit phase. This is where it finally touches the real DOM.
React applies each mutation in the patch — updating attributes, inserting nodes, removing nodes — using the browser's native DOM APIs. The browser then re-paints only what changed.
After the commit phase completes, your screen shows the updated UI. The new Virtual DOM tree now becomes the "current" tree, ready to be compared against the next render.
Why This Approach Improves Performance
Let's be clear about something: the Virtual DOM itself doesn't make things faster than a perfectly optimized vanilla JS update. A skilled developer who knows exactly which DOM nodes to update could theoretically be just as fast.
But in practice, most applications are complex. Nobody wants to manually track which exact DOM node needs to change for every state update. That's error-prone and doesn't scale.
React's Virtual DOM gives you a declarative programming model — you describe what the UI should look like, and React figures out how to get there.
You write: jsx
return <h1>{count}</h1>;
Not: js
document.getElementById('counter').textContent = count;
The performance win comes from:
Batching — React can batch multiple state updates and process them together in a single render cycle.
Selective updates — Only changed DOM nodes are touched, not the entire page.
No wasted work — Components whose props and state didn't change don't re-render their real DOM nodes.
A Quick Note on React Fiber
You might have heard of React Fiber, which is React's internal reconciler (introduced in React 16). Fiber is essentially a rewrite of the diffing and scheduling system that makes React more intelligent about when and how to process updates — including the ability to pause, prioritize, and resume rendering work.
But here's the thing: Fiber doesn't change the mental model above. The Virtual DOM, diffing, and commit phase still work exactly the way described here. Fiber just makes the underlying machinery smarter. Understanding the model in this article gives you a solid foundation before you ever need to go deeper into Fiber.
