← All articles
React ConceptsVirtual DOMBeginner

What Is Virtual DOM? Virtual DOM vs Real DOM Explained

What the DOM actually is, what the Virtual DOM adds on top of it, and why React uses one — with the diffing and patching steps explained in plain terms, not just buzzwords.

Kumar Astik· MERN Developer8 min read

"Virtual DOM" gets thrown around in React tutorials as if saying it explains anything. It doesn't, on its own — it's just a name. To actually understand what it does, you need to start with the thing it's virtualizing: the real DOM, and why touching it directly is the part everyone's trying to avoid.

What Is the DOM, Actually?

DOM stands for Document Object Model. When a browser loads your HTML, it doesn't keep it as a string of text — it parses it into a tree of objects, with a node for every element, attribute, and piece of text on the page. That tree is the DOM, and it's what JavaScript actually reads and modifies when you call something like document.getElementById or change an element's text.

The catch is that the DOM isn't just a data structure sitting quietly in memory. It's wired directly into the browser's rendering pipeline. Change a node's size or content, and the browser may need to recalculate layout for everything around it, then repaint the affected pixels. Do that constantly — say, once per keystroke, or once per tick of a live counter — and the layout and paint work starts to add up, even though each individual change looks trivial.

This is where the framing "what is DOM in React" gets a little misleading. React doesn't introduce a different DOM. Your app still ends up as real elements in the real browser DOM. What React changes is how often, and how directly, your code writes to it.

What Is the Virtual DOM?

The Virtual DOM is a plain JavaScript object tree that mirrors the shape of the real DOM, minus all the browser machinery attached to it. Every element becomes a lightweight description — its type, its props, its children — with no layout engine, no paint step, no rendering cost at all. Creating one, throwing it away, and creating another is cheap, because it's just object allocation.

That cheapness is the entire point. Instead of your component code writing to the real DOM directly every time state changes, React writes to this in-memory tree first. Then, before anything touches the actual page, React compares the new tree against the previous one and works out exactly what's different.

How the Virtual DOM Actually Works

Three steps happen every time a component's state or props change, and the order matters — most of the performance story lives in step two, not step one or three.

1. Render — build the new virtual tree

When state updates, React re-runs your component function and builds a fresh Virtual DOM tree describing what the UI should look like now. This step doesn't touch the browser at all — it's pure JavaScript object creation, which is why re-rendering a component isn't automatically expensive the way people assume.

2. Diff — compare old tree to new tree

React then compares the new virtual tree to the one from the last render, node by node, looking for what actually changed. If a node's type stayed the same, React checks its props and children for differences instead of throwing the whole thing away. If the type changed entirely — a div swapped for a span, say — React treats it as a full replacement rather than trying to patch it, since there's rarely a clean way to reuse one for the other.

3. Patch — apply only the real changes

Once the diff is done, React takes the (usually small) list of actual differences and applies just those to the real DOM, batched into as few operations as it can manage. This is the step that used to run once per state change with direct DOM manipulation; with the Virtual DOM in between, it runs once per render cycle no matter how many individual state updates fed into it.

// Before: count is 3
const before = { type: "p", props: { children: "Count: 3" } };

// After a click, count is 4 — only the text changed,
// so React patches this one text node instead of
// rebuilding the <p> element from scratch
const after = { type: "p", props: { children: "Count: 4" } };

Virtual DOM vs Real DOM: The Actual Differences

  • Cost per change — the real DOM triggers layout and paint work on relevant updates; the Virtual DOM is just an in-memory object diff with no rendering cost
  • Batching — direct DOM code applies each change as it happens; React collects changes across a render and applies them together
  • Disposability — a Virtual DOM tree can be built and discarded many times per second with no visible cost; doing that to the real DOM would be janky and slow
  • What actually reaches the browser — no matter how many virtual re-renders happen, only the final, diffed set of changes gets written to the real DOM

Is the Virtual DOM Always Faster?

Not by default, and it's worth being honest about that instead of repeating the marketing version. Building and diffing a Virtual DOM tree has its own cost — for a page that changes rarely, hand-written direct DOM updates can outperform a framework doing a full diff for one small change. The Virtual DOM doesn't make any single update instant; it makes frequent, repeated updates cheaper in aggregate, by preventing dozens of redundant layout/paint cycles from ever happening.

In other words, the win shows up in apps that update constantly and unpredictably — live data, forms, drag interactions, anything state-heavy — not in a static page that renders once and sits there.

Why React Specifically Uses This Model

React's component model encourages describing UI as a function of state — you write what the UI should look like for any given state, not the individual steps to mutate it from one state to another. That's a natural fit for the Virtual DOM: your component just returns a fresh description every render, and React handles figuring out the minimal set of real changes underneath. Without that layer, every component would need to manually track what changed and write imperative DOM-update code for it — which is exactly the tedious, error-prone pattern component frameworks were built to get away from.

"The Virtual DOM doesn't make updates free. It makes the expensive part — touching the real page — happen as rarely as possible."

Common Misunderstandings Worth Clearing Up

Two mix-ups come up constantly. First, the Virtual DOM isn't a separate rendering engine or a second browser-like thing living somewhere — it's just plain JavaScript objects, no more special than any other object literal in your code. Second, "React re-renders the component" and "React writes to the DOM" are not the same event — a component can re-render (step 1) many times while the real DOM only gets touched (step 3) when the diff actually finds a difference.

Want to see this play out instead of just reading about it? Try the useState practice challenges on ReactGrind →

The Practical Takeaway

You don't need to think about the Virtual DOM day-to-day — React handles the diffing and patching without you writing a line of that logic yourself. What's worth internalizing is the mental model: describe the UI for the current state, let React figure out the delta, and stop reaching for direct DOM manipulation (like manually grabbing a node with a ref just to change its text) unless you have a specific reason React's own state model can't cover.

Frequently asked questions

What is the Virtual DOM?

The Virtual DOM is a lightweight JavaScript object that mirrors the structure of the real DOM. Instead of writing directly to the browser's DOM every time something changes, React updates this in-memory copy first, compares it to the previous version, and only pushes the actual differences to the real DOM.

What is DOM in React?

In React, "DOM" still refers to the same Document Object Model every browser uses — the tree of nodes representing your HTML page. React doesn't replace the DOM; it manages how and when your app writes to it, using the Virtual DOM as a buffer in between your component code and the actual page.

What's the difference between Virtual DOM and Real DOM?

The real DOM is the browser's actual rendered page — updating it triggers layout recalculation and repainting, which is expensive. The Virtual DOM is a plain JavaScript representation with none of that overhead, so React can create it, diff it, and discard it as many times as needed before touching the real DOM even once.

What does DOM stand for?

DOM stands for Document Object Model. It's the browser's internal, structured representation of your HTML document as a tree of objects that JavaScript can read and modify. "Virtual DOM" borrows the name because it models that same tree, just outside the browser.

Is the Virtual DOM always faster than the Real DOM?

Not automatically — the Virtual DOM doesn't make any single update faster on its own. What it speeds up is repeated, frequent updates, by batching many changes into one real-DOM write instead of many. For a page that rarely changes, direct DOM manipulation can still be perfectly fast.

About the author
Kumar AstikMERN Developer
Keep reading