JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
Prev

Learn the concept

React Internals

react
junior
internals

What is the Virtual DOM and how does it work?

virtual-dom
reconciliation
dom
rendering
performance
diffing
Quick Answer

The Virtual DOM is a lightweight JavaScript object representation of the real DOM. When state changes, React creates a new virtual DOM tree, diffs it against the previous one, and applies only the minimal necessary changes to the actual DOM.

Detailed Explanation

What is the Virtual DOM?

The Virtual DOM (VDOM) is a programming concept where a virtual representation of the UI is kept in memory as plain JavaScript objects. Each React element — created by JSX or createElement() — is a simple object describing a DOM node:

JS
{ type: 'div', props: { className: 'card', children: [...] } }

These objects form a tree that mirrors the real DOM structure but is much cheaper to create and manipulate than actual DOM nodes.

Why Does React Use It?

Direct DOM manipulation is expensive. Every time you change a DOM node, the browser may need to recalculate styles, recompute layout, repaint pixels, and recomposite layers. The Virtual DOM lets React batch and minimize these operations.

How the Update Cycle Works:

  1. State changes trigger a re-render
  2. React calls your component function, which returns a new VDOM tree
  3. React diffs the new tree against the previous one (this is called reconciliation)
  4. React computes the minimal set of DOM operations needed
  5. React batches these operations and applies them to the real DOM in one pass

The Two Heuristics:

React's diffing algorithm achieves O(n) complexity using two assumptions:

  1. Different types produce different trees — If an element changes from <div> to <span>, React tears down the entire old subtree and builds a new one. It does not attempt to match children across different types.

  2. Keys identify stable elements — When diffing children of the same parent, React uses key props to match elements across renders. This is why keys are critical for lists — without them, React falls back to index-based matching which breaks with reordering.

Important Nuances:

  • The Virtual DOM is not a specification — it is React's implementation strategy. Other frameworks like Vue also use virtual DOMs, while Svelte compiles away the abstraction entirely.
  • React Fiber (since React 16) replaced the old recursive VDOM diffing with an incremental, interruptible approach using a linked-list tree structure.
  • The VDOM is not always faster than direct DOM manipulation for simple cases — its value is in making complex UIs with frequent updates manageable and predictable.

Code Examples

What a React element object looks likeJavaScript
// JSX
const element = <h1 className="title">Hello</h1>;

// Compiles to a plain JavaScript object:
const element = {
  type: 'h1',
  props: {
    className: 'title',
    children: 'Hello'
  }
};

// Nested elements create a tree of these objects:
const card = (
  <div className="card">
    <h2>Title</h2>
    <p>Description</p>
  </div>
);

// Becomes:
const card = {
  type: 'div',
  props: {
    className: 'card',
    children: [
      { type: 'h2', props: { children: 'Title' } },
      { type: 'p', props: { children: 'Description' } }
    ]
  }
};

Real-World Applications

Use Cases

Real-Time Dashboard Updates

Dashboards with frequently changing data rely on the VDOM to efficiently update only the cells or charts that changed, not the entire page.

Chat Application Messages

When new messages arrive, the VDOM ensures only the new message elements are added to the DOM while existing messages remain untouched.

Mini Projects

VDOM Visualizer

intermediate

Build a component that renders the VDOM tree as a visual diagram, highlighting which nodes change on each state update.

Direct DOM vs React Performance Comparison

beginner

Create the same dynamic list with both raw DOM manipulation and React, measuring DOM operation counts to see the VDOM's efficiency.

Industry Examples

Meta

React's Virtual DOM was created at Facebook to handle the complexity of News Feed updates, where thousands of elements change in response to real-time data.

Preact

Preact implements the same Virtual DOM concept in only 3KB, proving the pattern can be lightweight.

Resources

React Docs - Preserving and Resetting State

docs

MDN - Document Object Model (DOM)

docs

Related Questions

How does React's reconciliation algorithm work?

senior
internals

Explain React.memo, useMemo, and useCallback. When should each be used?

senior
performance

Why are keys important when rendering lists in React?

junior
lists
Previous
What is unidirectional data flow in React and why is it important?
Prev