React's Virtual DOM vs. the Real DOM

Image by engin akyurt.

The Document Object Model (DOM) is at the heart of every webpage, acting as the interface between JavaScript and the page's content. However, direct DOM updates can be slow and inefficient, leading to performance issues in complex applications.

React solves this problem by introducing the virtual DOM. Instead of making changes to the real DOM immediately, React updates a virtual representation first, applying only the necessary updates. This makes rendering more efficient and keeps applications running smoothly.

In this article, I explore the differences between the real DOM and the virtual DOM, how React's reconciliation process works, and when the virtual DOM is most useful.


What is the Real DOM?

The real DOM (Document Object Model) is a structured representation of a webpage, created by the browser when it loads an HTML document. JavaScript can interact with this structure to update elements dynamically.

How the Real DOM Works

When JavaScript modifies an element, such as changing text or updating a style, the browser applies these changes directly to the DOM and rerenders the affected parts of the page.

For example:

document.getElementById("title").textContent = "Updated Title";

This updates the <h1> element in the DOM. The browser may then recalculate styles, layout or paint according to what changed; a text update does not intrinsically require every stage.

Why the Real DOM Can Be Slow

DOM mutation and the browser work that may follow have different costs. Depending on the change:

  1. The browser recalculates styles to determine how the change affects the layout.
  2. The affected elements are reflowed, adjusting their position on the page.
  3. The screen is repainted, which can be expensive, especially for complex pages.

If too many updates happen in quick succession, such as in animations or interactive applications, performance can degrade very noticeably.


What is the Virtual DOM?

The virtual DOM (VDOM) is a lightweight copy of the real DOM which React uses to improve performance. Instead of modifying the DOM directly, React:

  1. Creates a virtual DOM tree

    that mirrors the structure of the real DOM.
  2. Updates the virtual DOM

    whenever state or props change.
  3. Compares the new virtual DOM with the previous one

    to determine what has changed.
  4. Applies only the necessary updates to the real DOM

    , avoiding unnecessary rerenders.

How the Virtual DOM Works

Consider this React component:

const Title = ({ text }: { text: string }) => {  return <h1>{text}</h1>;};

If text changes from "Hello" to "Welcome", React does not immediately update the real DOM. Instead, it:

  1. Updates the virtual DOM, creating a new version of the tree.
  2. Compares the new tree to the previous version, detecting that only the text has changed.
  3. Updates just the text in the real DOM, rather than replacing the entire <h1> element.

This process lets React calculate a host update before committing it. Whether it improves performance depends on the component work, the comparison cost and the browser work avoided.


Virtual DOM vs. Real DOM: Key Differences

FeatureReal DOMVirtual DOM
UpdatesModifies elements directlyUpdates a virtual copy first
PerformanceCost depends on the mutation and browser work it invalidatesAdds render and comparison work; batching can reduce commits
EfficiencyCan be efficient when updates are already targetedCan avoid host mutations after reconciliation
RenderingOnly invalidated browser work is performedReact renders and compares before committing host changes

The virtual tree gives React a declarative way to calculate updates. It can avoid some host mutations, but it is not automatically faster than targeted DOM work and still has its own rendering and comparison cost.


How React Uses Reconciliation

To apply changes efficiently, React uses reconciliation, a process that compares the new virtual DOM with the previous version and determines the minimal set of updates needed.

The Diffing Algorithm

React determines what has changed by diffing the new virtual DOM against the old one.

  1. If an element's type remains the same, React updates it in place.
  2. If an element's type changes, React removes the old one and inserts a new one.
  3. If elements are in a list, React uses keys to track their positions and avoid unnecessary rerenders.

For example:

const List = ({ items }: { items: string[] }) => (  <ul>    {items.map((item) => (      <li key={item}>{item}</li>    ))}  </ul>);

If items in the list are reordered but do not have unique keys, React may have to destroy and recreate them all rather than simply reordering. Using stable keys ensures React updates only what is necessary.


When is the Virtual DOM Most Beneficial?

This abstraction is most useful in applications that require frequent UI updates, such as:

  • Singlepage applications (SPAs)

    where components update dynamically without full page reloads.
  • Realtime dashboards

    that display constantly changing data.
  • Complex component trees

    where direct DOM updates would be inefficient.
  • Animations and interactive elements

    that require smooth rendering.

In these cases, React's approach can make complex statedriven updates easier to express. Its performance should still be measured against the work the application performs.


Wrapping Up

React renders a virtual tree, reconciles it with the previous result and then commits the required host changes. This model can avoid unnecessary DOM mutations, but it does not prevent component renders or guarantee better performance; the result depends on the work performed.

Key Takeaways

  • The real DOM is the browser's representation of an HTML document.
  • The virtual DOM is React's inmemory description of the UI, not a byteforbyte copy of the browser DOM.
  • The real DOM can be slow when a change invalidates substantial style, layout or paint work; a virtual tree can avoid some host mutations but adds rendering and comparison work.
  • React's diffing algorithm uses heuristics to decide which host changes to commit; this is not a universal performance guarantee.
  • The virtual DOM is particularly useful as a declarative programming model for complex, statedriven interfaces; its performance benefit is workloaddependent.

Understanding how React's virtual DOM works can help us write more efficient applications and optimise performance in the right places.


Looking for technical direction?

I support teams that need senior judgement on React, Next.js, headless CMS architecture, performance, migrations, and technical SEO.