Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
The virtual DOM explained: how React re-renders, diffs and patches the real DOM, why keys matter, and how Svelte, Solid and signals take a different path.
Quick answer: The virtual DOM is a lightweight copy of the page's structure, held in memory as plain JavaScript objects. In React, when your data changes, your components run again and produce a new virtual tree. React compares it with the previous one (diffing), works out the smallest set of changes, and applies only those to the real DOM. The purpose was never raw speed. It lets developers write the interface as a simple function of state, "given this data, the screen looks like this", and leave the tricky job of updating the real page efficiently to the library.
Also Read: How CORS Works and Why It Blocks Your Requests - Web Development
Before libraries like React, updating a page meant manual DOM manipulation:
// Something changed. Now find and update every affected part by hand.
document.querySelector("#count").textContent = items.length;
const li = document.createElement("li");
li.textContent = newItem.name;
document.querySelector("#list").appendChild(li);
if (items.length > 0) document.querySelector("#empty").remove();
Each change to the data required the right matching changes to the page. As applications grew, the data and the screen drifted out of step, producing bugs like a counter that says 3 above a list of 4.
The simplest fix would be to throw the whole page away and rebuild it from the data on every change. That is always correct, and far too costly: rebuilding the real DOM is slow, and it destroys scroll position, focus and whatever the user was typing.
The virtual DOM gives you the simplicity of "rebuild everything" at the cost of "change only what is needed".
With React, you describe what the interface should look like for any given state:
function TodoList({ items }) {
return (
<div>
<p>{items.length} items</p>
{items.length === 0 ? (
<p>Nothing to do</p>
) : (
<ul>
{items.map(item => <li key={item.id}>{item.name}</li>)}
</ul>
)}
</div>
);
}
There are no instructions for how to get from one screen to the next. This is declarative programming, summed up as UI = f(state).
That JSX is not HTML. It compiles to function calls that return plain objects describing elements:
{ type: "p", props: { children: "3 items" } }
A tree of such objects is the virtual DOM. Creating thousands of small objects is cheap. Touching the real DOM can trigger style recalculation, layout and paint; see how browsers render a web page.
The React documentation on render and commit describes the cycle.
setState.Note the terminology: in React, "render" means calling your component functions, not drawing to the screen.
Comparing two arbitrary trees perfectly is very expensive. React's reconciliation algorithm uses two assumptions to make it fast, taking time proportional to the number of elements.
1. Elements of different types produce different trees. If a <div> becomes a <span>, or one component type is replaced by another, React does not look for similarities. It discards the old subtree and builds a new one.
2. Elements of the same type are updated in place. React keeps the existing DOM node and changes only the attributes that differ.
For lists, it needs help. That is what key is for.
Suppose a list has items A, B, C, and a new item Z is added at the front.
Without keys, React compares by position: slot 1 was A and is now Z, slot 2 was B and is now A, and so on. It rewrites every item and adds one at the end.
With stable keys, React matches by identity: A, B and C are unchanged, and Z is new. It inserts one node.
{items.map(item => <li key={item.id}>{item.name}</li>)}
Two rules:
This is the most common misunderstanding. The virtual DOM is not faster than well-targeted manual DOM updates. It adds work: building a new tree and comparing it with the old one.
Also Read: Cookies vs LocalStorage vs Sessions: Key Differences
What it is faster than is the naive alternative of rebuilding the real DOM every time. Its real value is good enough performance with a much simpler programming model.
The cost becomes noticeable when large trees re-render often. When a component's state changes, React by default re-runs that component and all of its descendants. Developers manage this with:
React.memo, to skip a component whose props have not changed.useMemo and useCallback, to keep values and functions stable between renders.React's own compiler now automates much of this memoisation.
These checks depend on comparing references, which is why React asks you to treat state as immutable: a changed value must be a new object. See why immutability makes code easier to reason about.
Rich Harris, the creator of Svelte, argued in Virtual DOM is pure overhead that diffing is unnecessary work if you can know in advance what will change.
| Approach | How updates happen | Examples |
|---|---|---|
| Virtual DOM | Re-run components, diff trees, patch | React, Preact, Vue (with compiler optimisations) |
| Compile-time | A compiler turns components into code that updates exactly the right DOM nodes | Svelte |
| Fine-grained reactivity (signals) | Each piece of state tracks which DOM nodes depend on it and updates only those | SolidJS, Vue's reactivity, Angular signals, Svelte 5 |
Signals are the notable trend. A signal is a value that knows who reads it. When it changes, only the specific text nodes or attributes that depend on it are updated, with no component re-run and no tree comparison.
The trade-offs are real on both sides. The virtual DOM gives a simple mental model, in which a component is just a function, and makes it easy to render to targets other than the browser, such as native mobile views. Signals and compilers do less work at run time and ship less code, in exchange for some extra rules about how reactivity behaves.
For most applications, any of these is fast enough. The choice is more often about ecosystem, team experience and tooling.
An in-memory representation of the page as JavaScript objects. A library compares a new version with the old one and updates only the parts of the real page that changed.
Not inherently. It adds overhead compared with precise manual updates. Its benefit is letting you write simpler code while keeping updates efficient enough.
Keys let React identify which list items are the same between renders, so it can move, keep or remove the right ones instead of rewriting them all.
No. Svelte compiles components to direct DOM updates, and SolidJS and others use signals for fine-grained updates.
The virtual DOM made it practical to describe an interface as a function of its data and let the library work out the changes. It was a means to that end, not a performance trick. Newer approaches reach the same goal with less run-time work, but the central idea React popularised, declarative UI driven by state, is now how almost every front-end framework works.