React's Virtual DOM vs. the Real DOM

Hero image for React's Virtual DOM vs. the Real DOM. Image by engin akyurt.
Hero image for '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 real DOM, triggering the browser to recalculate styles, adjust the layout, and repaint the affected section of the page.

Why the Real DOM Can Be Slow

The real DOM was not designed for frequent updates. Each time an element changes:

  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 ensures that updates to the real DOM are applied efficiently, improving performance.


Virtual DOM vs. Real DOM: Key Differences

FeatureReal DOMVirtual DOM
UpdatesModifies elements directlyUpdates a virtual copy first
PerformanceSlower, as every change triggers layout recalculationsFaster, as React batches changes
EfficiencyExpensive for frequent updatesReduces unnecessary DOM manipulation
RenderingChanges affect the entire treeOnly modified elements are updated

By acting as an intermediary, the virtual DOM makes UI updates significantly faster, especially in complex applications.


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?

The virtual DOM 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 significantly reduces the overhead associated with direct DOM manipulation.


Wrapping up

React's virtual DOM improves performance by minimising direct interactions with the real DOM. Instead of applying changes immediately, React updates the virtual DOM first, then efficiently applies only the necessary updates. This prevents unnecessary rerenders and keeps applications running smoothly.

Key Takeaways

  • The real DOM is the browser's representation of an HTML document.
  • The virtual DOM is a lightweight copy that React updates in memory before applying changes.
  • The real DOM can be slow when updated frequently, whereas the virtual DOM reduces unnecessary changes.
  • React's diffing algorithm ensures that only modified elements are updated efficiently.
  • The virtual DOM is especially beneficial for complex applications that need frequent updates.

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


Categories:

  1. Development
  2. Front‑End Development
  3. Guides
  4. JavaScript
  5. JSX
  6. React