Virtual DOM
- In Turkish
- Sanal DOM
- Pronunciation
- VUR-choo-ul DAHM
In short
The virtual DOM is a lightweight copy of a page's element tree kept in JavaScript memory, which UI libraries compare to update the real DOM efficiently.
What is the virtual DOM?
The virtual DOM is a representation of the user interface made of plain JavaScript objects that mirrors the structure of the real DOM, the browser's tree of page elements. Instead of changing the page directly, a UI library such as React or Vue builds a new virtual DOM tree whenever the app's data changes and then works out what actually needs to change on the page.
The process has three steps. First, the library renders a new virtual tree from the current state; second, it compares that tree with the previous one, a step called diffing (React calls the whole process reconciliation). Third, it applies only the differences to the real DOM, for example updating the text of one list item instead of rebuilding the whole list, and keys such as React's key prop help it match list items that moved.
It's like an architect editing a blueprint and then sending the builders a short list of changes, instead of tearing down and rebuilding the house after every revision. The main benefit is for developers: you describe what the UI should look like for a given state, and the library figures out the DOM operations, which avoids many bugs that come from updating the page by hand.
A common misconception is that the virtual DOM is faster than the real DOM. Carefully targeted direct DOM updates are faster, because diffing adds extra work; the virtual DOM is a trade-off that keeps updates reasonably fast while letting you write declarative code, and frameworks such as Svelte and Solid skip it entirely by compiling components or tracking fine-grained changes. The virtual DOM is also unrelated to the shadow DOM, a browser feature that isolates a web component's markup and styles.
Key takeaways
- The virtual DOM is an in-memory JavaScript copy of the page's element tree.
- Libraries diff the new virtual tree against the old one and update only what changed.
- Its main benefit is declarative code, not raw speed over hand-written DOM updates.
- Keys help the diffing algorithm track list items that are added, removed, or moved.
- It is different from the shadow DOM, which isolates web component markup and styles.
Example
// A virtual DOM node is just a plain object describing an element
const oldTree = { type: "ul", children: [
{ type: "li", key: "a", text: "Milk" },
{ type: "li", key: "b", text: "Eggs" },
]};
const newTree = { type: "ul", children: [
{ type: "li", key: "a", text: "Milk" },
{ type: "li", key: "b", text: "Eggs (12)" }, // only this changed
]};
// Diffing finds one change, so only one real DOM update is needed:
// secondListItem.textContent = "Eggs (12)";Readers ask
Is the virtual DOM faster than the real DOM?
Not by itself. Diffing adds work compared with perfectly targeted DOM updates; the virtual DOM's value is that it keeps updates reasonably efficient while letting you write simple, declarative UI code.
What is the difference between the virtual DOM and the shadow DOM?
The virtual DOM is a JavaScript copy of the UI that libraries use to calculate updates. The shadow DOM is a browser feature that gives a web component its own isolated tree of elements and styles.
Which frameworks use a virtual DOM?
React and Preact use a virtual DOM, and so does Vue by default. Frameworks such as Svelte and Solid avoid it by compiling components or tracking fine-grained changes, so they update exactly the DOM nodes that depend on changed data.
Often compared
See also
- DOMWeb Development, p. 13The DOM is the browser's in-memory tree of objects representing a web page, which JavaScript can read and change to update what the user sees.
- ReactWeb Development, p. 36React is an open-source JavaScript library for building user interfaces from reusable components that update automatically when their data changes.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- SPAWeb Development, p. 41An SPA is a web application that loads a single HTML page and then updates content with JavaScript, so moving between views does not reload the page.
- HydrationWeb Development, p. 22Hydration is the step where JavaScript in the browser takes over server-rendered HTML, attaching event handlers and state so the page becomes interactive.
- CSRWeb Development, p. 9CSR is a rendering approach where the browser downloads a mostly empty HTML page plus JavaScript, then builds the page content on the user's device.
Spotted a mistake or something missing on this page?Suggest an edit