← Back to React, page 6

react-0101 · Easy fresher

Is the virtual DOM faster than the real DOM? Explain the common myths about it.

No. The virtual DOM is not faster than well-written direct DOM updates, because it adds work on top of them: React builds a tree of plain objects, compares it with the previous one and then updates the real DOM. Its value is that you describe the UI as a function of state and React finds a reasonably small set of DOM changes for you. It is a productivity and predictability tool, and the cost is acceptable, not zero.

Full answer

The phrase "the virtual DOM makes React fast" is repeated so often that it has become a myth. The accurate statement is narrower.

What a virtual DOM is. When a component renders, it returns React elements, which are plain JavaScript objects such as { type: 'p', props: { className: 'note' } }. They are cheap to create. React compares the new tree with the previous one (reconciliation) and applies only the differences to the real DOM.

Myth 1: it is faster than the DOM. Any change React makes to the DOM could be written by hand with equal or better speed, because React does the same DOM operations plus the cost of creating and comparing objects. The real comparison is with the alternative of re-creating all DOM nodes on every change, which would be slow and would lose focus, scroll position and input state.

Myth 2: React never touches unchanged DOM. This is mostly true for text and attributes, but React still re-runs component functions and compares trees even when nothing changed. That work is what memo, state colocation and the React Compiler reduce.

Myth 3: the virtual DOM is why React exists. React's core idea is declarative UI: you write what the screen should look like for the current state, and React figures out how to get there. The virtual DOM is one implementation technique. Other libraries reach the same goal differently: Svelte compiles updates at build time, and Solid and Vue's reactivity system track which exact values changed, so they skip the tree comparison.

Myth 4: the virtual DOM is a copy of the browser DOM. It is not. It is a lightweight description with no layout, no event system and no methods. React's internal Fiber tree, which also stores state and effects, is separate from the elements.

Where React is slow in practice is rarely the DOM write. It is rendering too many components, expensive work inside render, and large lists. Profile before optimizing.

A good interview answer: the virtual DOM is a trade-off that gives a simple programming model with good-enough performance, and React keeps adding tools (batching, transitions, compiler) to reduce the cost.

The example shows that an element is a plain object, and that a state change touches one text node.

jsx
import { useState } from 'react';
import { createRoot } from 'react-dom/client';

const element = <p className="note">Hello</p>;
console.log('element type: ' + element.type + ', className: ' + element.props.className + ', plain object: ' + (Object.getPrototypeOf(element) === Object.prototype));

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>Static text</p>
      <button onClick={() => setCount(count + 1)}>Clicked {count}</button>
    </div>
  );
}

const container = document.getElementById('root');
createRoot(container).render(<Counter />);
await new Promise((resolve) => setTimeout(resolve, 30));

const mutations = [];
new window.MutationObserver((records) => records.forEach((r) => mutations.push(r.type))).observe(container, {
  subtree: true,
  childList: true,
  characterData: true,
  attributes: true,
});
container.querySelector('button').click();
await new Promise((resolve) => setTimeout(resolve, 30));
console.log('DOM mutations after one click: ' + JSON.stringify(mutations));
console.log('button text now: ' + container.querySelector('button').textContent);

This ran as a script. A React element is a plain object. After a click the component function ran again, but the real DOM received one text change, and the "Static text" paragraph was not touched. Output of the run: element type: p, className: note, plain object: true DOM mutations after one click: ["characterData"] button text now: Clicked 1

Key points

  • The virtual DOM adds work, so it is not faster than careful manual DOM updates.
  • Its benefit is declarative code: describe the UI for the current state and let React compute the changes.
  • Elements are plain objects, not a copy of the browser DOM.
  • Other libraries (Svelte, Solid, Vue) avoid tree comparison with compile-time or fine-grained reactivity.
  • Real slowness usually comes from rendering too much, not from DOM writes. Measure first.

Common mistakes

  • Saying the virtual DOM is faster than the DOM.
  • Describing the virtual DOM as a copy of the browser DOM.
  • Believing React never re-runs work for unchanged parts of the tree.
  • Optimizing DOM writes while ignoring expensive renders.
  • Treating the virtual DOM as the main reason to choose React.

Follow-ups

  • How do Svelte and Solid update the DOM without a virtual DOM?
  • What is the difference between React elements and the Fiber tree?
  • What does the React Compiler change about unnecessary renders?

Tips

  • Open with "no, and here is what it actually gives you". It shows you can challenge a popular claim.
  • Name an alternative approach, such as compile-time updates in Svelte.
  • Finish with measuring and the real causes of slowness.