Frontend

What is Virtual DOM?

A lightweight JavaScript copy of the real DOM used by frameworks like React for efficient updates.

Definition

The Virtual DOM is an in-memory representation of the real DOM. React and similar frameworks use it to batch and optimize UI updates. When state changes, a new Virtual DOM tree is created and compared (diffed) with the previous one. Only the differences are applied to the real DOM in a minimal set of operations. This 'reconciliation' process makes updates more efficient than direct DOM manipulation.

A diff, step by step

Take a list rendered from state:

function Basket({ items }) {
  return <ul>{items.map((item) => <li key={item.id}>{item.name}</li>)}</ul>;
}

When items goes from [Apples, Bananas] to [Cherries, Apples, Bananas], React renders a new tree and compares it with the previous one. Matching children by key, it sees that Apples and Bananas are unchanged and only Cherries is new, so the real DOM gets one insert. With the array index as the key, the new items line up against the old ones by position instead: React rewrites the text of both existing <li> elements and appends a third, three DOM writes instead of one, and any state inside those rows ends up attached to the wrong item.

Virtual DOM, signals and compiled output

ApproachUsed byWhat happens when state changes
Virtual DOM diffingReact, Preact, VueThe component re-runs, the new tree is diffed against the old one, and only the differences are patched into the DOM
Fine-grained signalsSolidJS, Preact Signals, Angular signalsOnly the DOM nodes bound to the changed value update; no tree is rebuilt or diffed
Compile-time updatesSvelteThe compiler emits code that updates exactly the nodes that depend on the changed value

What it does not do

The virtual DOM is not faster than well-written direct DOM updates. It trades some CPU for a declarative model in which you describe the UI for a given state and let the library work out the changes. The cost shows up in large trees that re-render on every change, where the diff runs even when little changed. Stable keys, memoization with React.memo and useMemo, and the React Compiler cut that work by skipping subtrees whose inputs did not change.

When to Use Virtual DOM

You get a virtual DOM automatically with React, Preact or Vue; the reason to understand it is to keep updates cheap. Give list items stable keys from your data, keep state immutable so the diff can see changes, and memoize expensive subtrees. If a UI is dominated by many small, frequent updates, such as a live dashboard or a spreadsheet grid, a signals-based framework can do the same work with less overhead.

Virtual DOM in Practice

  • Stable keys from your data, not the array index: items.map((item) => <li key={item.id}>{item.name}</li>)
  • Skip re-rendering a row whose props did not change: const Row = React.memo(function Row({ item }) { return <li>{item.name}</li>; });
  • Immutable update so React sees a new array: setItems((prev) => [newItem, ...prev]); // not prev.unshift(newItem)

Common Mistakes

  • Using the array index as the key in a list that can reorder or have items inserted, which attaches DOM nodes and state to the wrong items.
  • Mutating state in place, so the new value is the same object and the diff sees nothing to update.
  • Assuming the virtual DOM makes every update cheap; re-rendering a large tree still costs CPU before any DOM work happens.

Related Terms

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.