What Is Virtual DOM — Complete Guide
In this tutorial, you will learn about What Is Virtual DOM. We cover key concepts, practical examples, and best practices to help you master this topic.
The Virtual DOM is a lightweight JavaScript object tree that mirrors the real DOM, enabling frameworks to calculate minimal updates and batch changes efficiently.
What You'll Learn
- What the Virtual DOM is and why it was created
- How the Virtual DOM differs from the real DOM
- The performance problem it solves
- How frameworks use the Virtual DOM internally
Why It Matters
Direct DOM manipulation is slow and error-prone. The Virtual DOM provides a buffer between application state and the browser, enabling declarative UI programming without sacrificing performance.
flowchart LR A[Application State] --> B[Virtual DOM Tree] B --> C[Diff Engine] C --> D[Patch Instructions] D --> E[Real DOM Updates] E --> F[Browser Repaint] B -.->|Re-render on state change| A
What the Virtual DOM Is
The Virtual DOM is not a browser API. It is a programming pattern where a lightweight copy of the DOM is kept in memory as plain JavaScript objects. Each virtual node represents a real DOM element with its type, properties, and children.
// A real DOM element
const realDiv = document.createElement('div');
realDiv.className = 'container';
realDiv.textContent = 'Hello';
// Its virtual DOM representation
const virtualDiv = {
type: 'div',
props: {
className: 'container',
children: 'Hello'
}
};
// The virtual node is just a plain object.
// No browser API calls. No layout calculations.
// Creating it is near-instant.
The Performance Problem
Direct DOM manipulation triggers layout recalculations and repaints. Each change can cascade.
// Bad: Direct DOM manipulation causes multiple reflows
const list = document.getElementById('list');
console.time('directDom');
for (let i = 0; i < 100; i++) {
const item = document.createElement('li');
item.textContent = 'Item ' + i;
list.appendChild(item); // Reflow after each append
}
console.timeEnd('directDom');
// The browser recalculates layout 100 times.
// Each appendChild potentially triggers style recalculation,
// layout, and paint for the entire document.
How Virtual DOM Solves It
The Virtual DOM batches all changes and applies them in one efficient pass.
// Simplified virtual DOM approach
console.time('virtualDom');
const updates = [];
for (let i = 0; i < 100; i++) {
// Build virtual nodes (no DOM API calls)
updates.push({
type: 'li',
props: { children: 'Item ' + i }
});
}
// Single batch DOM update
const fragment = document.createDocumentFragment();
updates.forEach(vnode => {
const el = document.createElement(vnode.type);
el.textContent = vnode.props.children;
fragment.appendChild(el);
});
list.appendChild(fragment); // Single reflow
console.timeEnd('virtualDom');
// The fragment approach mimics what frameworks do:
// collect changes, then apply them in one batch.
Virtual DOM in Popular Frameworks
Different frameworks implement the Virtual DOM with different tradeoffs.
// Conceptual view of how frameworks use Virtual DOM
function frameworkRender(component, container) {
// 1. Create virtual tree from component state
const vdom = component.render();
// 2. Diff with previous virtual tree
const patches = diff(previousVdom, vdom);
// 3. Apply patches to real DOM
patch(container, patches);
// 4. Save current tree for next comparison
previousVdom = vdom;
}
// React, Vue, Preact, and others follow this pattern.
// The diff algorithm, tree structure, and batching strategy vary.
Common Mistakes
- Thinking the Virtual DOM is faster than direct DOM manipulation in every case (it adds overhead).
- Confusing the Virtual DOM with Shadow Dom (they solve different problems).
- Believing the Virtual DOM is a browser feature (it is a JavaScript pattern).
- Assuming all frameworks use the same Virtual DOM implementation.
- Thinking the Virtual DOM eliminates all DOM performance problems.
Practice Questions
- What is the Virtual DOM? A lightweight JavaScript object tree that mirrors the real DOM.
- Why was the Virtual DOM created? To batch DOM updates and minimize expensive reflows and repaints.
- Is the Virtual DOM part of the browser API? No. It is a programming pattern used by frameworks.
- What is the difference between Virtual DOM and Shadow DOM? Virtual DOM optimizes updates. Shadow DOM provides Encapsulation.
Challenge
Build a simple benchmark that compares creating 1000 DOM elements directly vs building a virtual node tree and applying it in one batch. Measure and display the time difference.
FAQ
Mini Project
Build a simple virtual node Factory function and a render function that converts virtual nodes to real DOM elements. Create a virtual tree representing a todo list. Render it to the page. Measure the time to create the virtual tree vs rendering it.
What's Next
Lesson 2: How Virtual DOM Works — Diffing Algorithm
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro