What is the Virtual DOM, Actually

Exploring what is the virtual dom, actually and common implementation patterns

1. The Problem

You have a UI. The user does something. The UI needs to change. The question is not whether to update the DOM - you must. The question is how to decide which parts of the DOM to update and how to express that decision in code.

The DOM is a live tree of objects. Every node has properties, children, event listeners, layout state, style computations. Touching it is expensive. Not because the browser engineers wrote bad code, but because a DOM mutation triggers a cascade: style recalculation, layout reflow, paint, composite. Touch the wrong node and you pay for all of it.

So the naive approach - “just re-render everything when anything changes” - is too slow for a UI with hundreds of nodes updating dozens of times per second. But the surgical approach - “manually track exactly which DOM nodes need to change and update only those” - puts that tracking burden on you, the developer. Your application code becomes intertwined with DOM manipulation logic. Business logic and rendering logic cannot be separated. Every feature carries the weight of its own update strategy.

There has to be a middle path: you describe what the UI should look like, and something else figures out what changed.


2. The Naive Attempt

Build a counter. The number on screen increments when you click a button.

First instinct: target the node and mutate it.

let count = 0;
const display = document.getElementById('count');
const button = document.getElementById('increment');

button.addEventListener('click', () => {
  count++;
  display.textContent = count;
});

This works. It is also the entire application. Now add a requirement: when the count exceeds 10, the display turns red. When it exceeds 20, a warning message appears below it.

button.addEventListener('click', () => {
  count++;
  display.textContent = count;

  if (count > 10) {
    display.style.color = 'red';
  }

  if (count > 20) {
    let warning = document.getElementById('warning');
    if (!warning) {
      warning = document.createElement('p');
      warning.id = 'warning';
      warning.textContent = 'Count is getting high.';
      display.parentNode.appendChild(warning);
    }
  }
});

Three requirements, and you are already tracking whether the warning node exists, manually creating and appending DOM nodes conditionally, imperatively mutating style. Now add the requirement that the warning disappears if count drops below 20. You need to find the node and remove it. Add a reset button. Add undo. Add server sync.

The code becomes a state machine implemented in imperative DOM mutations. Every possible state transition requires its own mutation sequence. Forgetting one leaves the DOM in a state that does not match your data. The DOM and your data model diverge. You cannot look at the DOM and know the application state without reading every mutation that led to it. You cannot look at your application state and know the DOM without running every mutation in sequence.

This is the scaling problem. It is not that getElementById is slow. It is that imperative DOM manipulation couples your state transitions to your rendering logic in a way that grows unmanageable.


3. Why That Breaks

The precise failure mode: imperative DOM manipulation requires you to track both the current state and every possible transition between states. Declarative rendering only requires you to describe the target state.

With imperative code, the DOM is the source of truth. To know what is on screen, you query the DOM. To change what is on screen, you write mutations. This creates two problems. First, the DOM is not a reliable source of truth - it is mutable global state that can be changed by anything with a reference to it. Second, mutations are order-dependent. You must apply them in the right sequence. Applying them out of order produces different results, and there is no compiler to catch it.

What you want is to describe the intended output - a tree of nodes with specific values - and have something else compute the difference from what is currently on screen, applying only the minimal mutations to close that gap. You describe the target. The system handles the transition.

The gap between describing and mutating is where the Virtual DOM lives.


4. React’s Mental Model

The Virtual DOM is not a performance optimization. It is a representation layer that lets you write declarative UI code.

You write what the UI should look like right now given the current state. React turns that description into a plain JavaScript object tree. When state changes, you produce a new description. React compares the old description to the new one, finds the differences, and applies only those differences to the actual DOM.

The key insight: comparing two JavaScript object trees is cheap. Comparing two DOM trees is expensive. So React keeps a lightweight JS copy of the DOM state, diffs the JS copies, and applies the minimal DOM mutations the diff reveals.

The Virtual DOM is that lightweight JS copy. It is not a performance layer bolted on top of React - it is the mechanism that makes declarative rendering possible at all. Without it, you cannot know what changed between renders without touching the DOM itself, which defeats the purpose.


5. React’s Solution

When you write JSX, you are not writing HTML. You are calling a function that returns a plain JavaScript object.

const element = (
  <div className="container">
    <p>Hello</p>
  </div>
);

After the JSX transform, this is:

const element = React.createElement(
  'div',
  { className: 'container' },
  React.createElement('p', null, 'Hello'),
);

And React.createElement returns this:

{
  $$typeof: Symbol(react.element),
  type: 'div',
  key: null,
  ref: null,
  props: {
    className: 'container',
    children: {
      $$typeof: Symbol(react.element),
      type: 'p',
      key: null,
      ref: null,
      props: { children: 'Hello' },
    }
  }
}

This is a Virtual DOM node. A plain JavaScript object. No browser APIs. No DOM references. Just data describing what a DOM node should look like.

Your entire component tree, when rendered, produces a tree of these objects. React holds two copies: the tree from the last render (the current state of the DOM) and the tree from the most recent render (what the DOM should become). The algorithm that compares them is reconciliation. The minimal set of DOM mutations it produces is the commit.

The split between these two phases is why React can interrupt rendering mid-tree and resume later (Concurrent Mode), something that would be impossible if rendering directly mutated the DOM. The Virtual DOM tree is just data. Building it is a pure computation. Applying it to the DOM is a separate, committed step.


6. Internals

The Virtual DOM object returned by React.createElement is not what React uses internally during reconciliation. It is an input. React converts it into a Fiber node.

A Fiber is a richer data structure that represents a unit of work:

Fiber {
  tag: HostComponent,        // type of fiber: function component, class, DOM node, etc.
  type: 'div',               // the element type
  key: null,
  pendingProps: { className: 'container' },
  memoizedProps: { className: 'container' },
  memoizedState: null,       // for function components: the hook list
  stateNode: <div />,        // the actual DOM node (once committed)
  return: Fiber,             // parent fiber
  child: Fiber,              // first child fiber
  sibling: Fiber,            // next sibling fiber
  alternate: Fiber,          // the previous version of this fiber
  flags: Update,             // what work needs to happen: Placement, Update, Deletion
  lanes: ...,                // priority scheduling metadata
}

React maintains two Fiber trees simultaneously: the current tree (what is committed to the DOM right now) and the work-in-progress tree (what is being built for the next render). Every Fiber node has an alternate pointer to its counterpart in the other tree.

When a render begins, React builds the work-in-progress tree by reconciling each element in the new Virtual DOM output against the corresponding Fiber in the current tree. If a node is the same type at the same position, React reuses the Fiber and updates its props. If the type changed, React creates a new Fiber and marks the old one for deletion. This comparison happens at the JavaScript level, against plain objects and Fiber metadata, not against the live DOM.

The result of this comparison is a set of effect flags on each Fiber: Placement (insert a new node), Update (modify an existing node), Deletion (remove a node). After the entire work-in-progress tree is built, React walks it in the commit phase and applies those effects to the real DOM in a single, synchronous pass. This is the only moment the real DOM is touched.

The Virtual DOM object you produce with React.createElement is therefore a short-lived intermediate. It is consumed immediately to create or update Fibers and then discarded. The Fiber tree is what React actually keeps, mutates, and reconciles across renders.


7. Interview Traps

Trap 1: “The Virtual DOM makes React faster than vanilla JS.” Is this true?

Answer

No, and this is one of the most persistent misconceptions in frontend development. A well-written vanilla JS application that surgically targets and mutates only the specific DOM nodes that need to change will always be faster than React for that specific operation. React’s Virtual DOM diffing has overhead: creating the Virtual DOM object tree on every render, running the reconciliation algorithm, then committing. Vanilla JS skips all of that.

The Virtual DOM is not a performance optimization over direct DOM manipulation. It is a performance optimization over the naive declarative approach of wiping and re-rendering the entire DOM subtree on every state change. The correct comparison is not React versus surgical vanilla JS. It is React versus innerHTML = renderEverything().

React’s value is not maximum possible performance. It is acceptable performance with a declarative, composable programming model that scales to complex applications. If you need absolute maximum rendering performance, you would not choose React. You would write targeted DOM mutations by hand. But for most applications, React’s performance is more than sufficient, and the developer experience tradeoff is worth it.


Trap 2: When you call setState, does React immediately update the Virtual DOM?

Answer

No. Calling setState (or the set function from useState) schedules a re-render. It does not immediately produce a new Virtual DOM tree. React batches state updates that occur within the same event handler, so multiple setState calls in one handler produce one re-render, not one per call. React 18 extends this batching to all async contexts via automatic batching.

The re-render - building the new Virtual DOM tree by calling your component function - happens in the render phase, which React schedules at an appropriate priority. In Concurrent Mode, this render phase can be interrupted, paused, or abandoned entirely if higher-priority work arrives. The Virtual DOM tree being built is just data in memory. Nothing is committed to the DOM until the render is complete and React enters the commit phase.

So the sequence is: setState → schedule → render phase (build new Virtual DOM tree, reconcile against current Fiber tree, compute effects) → commit phase (apply effects to real DOM). The DOM update happens at the end of the commit phase, not at the setState call site.


Trap 3: Two developers debate. One says “the Virtual DOM is just a copy of the DOM in JavaScript.” The other says “the Virtual DOM is an abstraction that describes what the DOM should look like.” Who is closer to correct, and what is the important distinction?

Answer

The second developer is closer to correct, and the distinction matters.

“A copy of the DOM in JavaScript” implies that the Virtual DOM is kept in sync with the real DOM at all times - that it is a mirror. This is wrong in two ways. First, the Virtual DOM objects produced by React.createElement are short-lived. They are created, consumed during reconciliation, and discarded. They are not maintained as a long-lived copy of the DOM. Second, what React does maintain long-term is the Fiber tree, which is not a copy of the DOM. It is a richer data structure that includes component state, effect lists, priority metadata, and references to the actual DOM nodes. The Fiber tree describes the component tree’s current committed state, not the DOM’s.

“An abstraction that describes what the DOM should look like” correctly frames the Virtual DOM as a description, not a mirror. You produce a description of the intended output. React uses it to compute what must change. The description is transient. The DOM is the ground truth of what is actually rendered. React’s job is to keep the DOM consistent with your latest description, using the Fiber tree as its working memory during that process.


Self-Test

Q1. What does React.createElement return, and what is the type of that return value?

Answer

React.createElement returns a plain JavaScript object called a React element. It has a fixed shape: $$typeof (a Symbol that identifies it as a React element and guards against XSS via JSON injection), type (the string tag name for DOM elements, or the function/class reference for components), key, ref, and props (an object containing all props including children). It is not a DOM node, not a Fiber, not a class instance. It is a data object. It has no methods, no reactivity, no connection to the browser. It is produced by calling your component function and is consumed immediately by the reconciler to build or update the Fiber tree.


Q2. If the Virtual DOM is not a performance optimization over vanilla JS, what problem does it actually solve?

Answer

It solves the problem of coupling between state transitions and rendering logic in declarative UI code. Without the Virtual DOM, a declarative rendering approach - “here is what the UI should look like for this state” - would require wiping and reconstructing the relevant DOM subtree from scratch on every state change. That is catastrophically slow because DOM construction, layout, and paint are expensive. The Virtual DOM interposes a cheap JS diffing step between your declaration and the DOM mutation: you produce a description, React diffs it against the previous description in JS (fast), and applies only the minimal mutations (targeted, minimal DOM writes). The Virtual DOM does not make React faster than surgical vanilla JS. It makes the declarative programming model viable at interactive frame rates by avoiding the full DOM reconstruction that naive declarative rendering would require.


Q3. What is the difference between a React element and a Fiber node? Why does React have both?

Answer

A React element is the output of React.createElement: a lightweight, immutable, short-lived plain object describing what a component or DOM node should look like for this render. It carries type, props, key, and ref. It has no state, no history, no link to the previous render. It is created fresh on every render and discarded after the reconciler processes it.

A Fiber node is the reconciler’s internal representation of a component instance across renders. It is mutable and persistent. It carries everything the reconciler needs to track work over time: the current and pending props, the component’s state (the hook linked list for function components), the actual DOM node reference once committed, pointers to parent, child, and sibling Fibers, the alternate pointer to its counterpart in the other tree, and effect flags describing what work needs to happen in the commit phase.

React has both because they serve different purposes. React elements are the developer-facing description API: you produce them, they are pure data, they are easy to reason about. Fibers are the engine’s working data structure: they carry everything needed to incrementally, interruptibly reconcile your descriptions against the real DOM over time. Conflating them - treating elements as if they persist, or treating Fibers as if they are developer-facing - leads to wrong mental models about when and how React tracks state and performs updates.


Q4. A developer argues that React’s Virtual DOM diffing is pure overhead for a simple static list that never changes. Are they right?

Answer

They are correct that React adds overhead a static list would not incur in vanilla JS. On every render, React produces the Virtual DOM element objects for the list, reconciles them against the current Fiber tree, determines no changes are needed, and exits without mutating the DOM. That reconciliation work - creating the element objects, running the comparison - has a cost in CPU time and memory allocation, even when the result is “nothing changed.”

For a genuinely static list that never updates, this is wasted work. A static HTML page or a vanilla JS render-once approach would be faster.

However, two practical qualifications. First, the overhead for a static list is typically measured in microseconds and is irrelevant for any application operating at normal scale. Second, React’s bailout mechanisms - React.memo, shouldComponentUpdate, the same-reference check on props - exist precisely to minimize this overhead when inputs have not changed. A list wrapped in React.memo receiving stable props will have its reconciliation short-circuited at the component boundary, skipping the full element-and-diff cycle for its subtree. The argument is theoretically correct but practically significant only in performance-critical scenarios where profiling has identified this specific overhead as a bottleneck.