How React Virtual DOM works under the Hood

If you’ve used React, Vue, or similar frontend libraries, you’ve probably encountered the term Virtual DOM. It’s often praised as a key performance optimization, but the specifics of how it works are sometimes left vague.
So let’s go beyond the buzzword and look at how the Virtual DOM actually functions — and what it’s doing behind the scenes when your UI updates.
The Real DOM
The real DOM is a tree structure maintained by the browser to represent the UI. It’s powerful but not particularly fast. Updating the DOM directly can be expensive due to operations like reflow and repaint, especially when updates happen frequently or involve large trees.
In pre-React days, developers had to manually orchestrate DOM changes: track what changed, when to update, and how to avoid unnecessary re-renders. This imperative approach often led to messy code and subtle bugs.
The Virtual DOM
The Virtual DOM (vDOM) is an abstraction — a lightweight JavaScript object that mirrors the structure of the real DOM. When you write JSX or TSX, you’re essentially declaring a UI structure that React turns into a virtual tree.
How It Works
When a component’s state or props change, React triggers a re-render, but instead of updating the actual DOM immediately, it follows a three-step process:
Render Phase: React creates a new Virtual DOM tree by calling the component’s render function. This is a pure, in-memory representation of the UI after the change.
Diffing Phase: React compares the new tree to the previous one using its reconciliation algorithm. It identifies exactly what has changed — whether it’s a node’s text, attributes, or position.
Commit Phase: React then generates a minimal set of DOM mutations (patches) and applies them to the real DOM in a single batch. This reduces layout thrashing and avoids unnecessary DOM writes.
This abstraction makes UI updates more efficient, but also predictable, which is why the Virtual DOM is such a foundational concept in modern frontend frameworks.
Final Thoughts
The Virtual DOM is not just a performance trick — it’s a model that enables declarative UI, predictable updates, and composable components. Understanding how it works helps you write more efficient, maintainable code — and gives you the tools to debug performance issues more effectively.
