AboutBlogContact
Frontend EngineeringJune 5, 2014 3 min read 122Updated: June 22, 2026

React's Virtual DOM: Why Diffing is the Future of UI (2014)

AunimedaAunimeda
📋 Table of Contents

React's Virtual DOM: Why Diffing is the Future of UI

It’s 2014, and Facebook has just open-sourced React. The initial reaction from the "old guard" of jQuery and Backbone developers has been... skeptical. "Why would I put my HTML inside my JavaScript?" they ask. But they are missing the forest for the trees. The real innovation isn't JSX; it's the Virtual DOM.

The DOM is Slow (Relatively)

Manipulating the actual DOM in a browser is expensive. Every time you change a class or update text content, the browser has to recalculate styles (Reflow) and repaint the screen. If you're building a complex app like Facebook's news feed, doing this hundreds of times a second will tank your frame rate.

How the Virtual DOM Works

React creates a lightweight JavaScript representation of the DOM tree. When your component's state changes, React builds a new Virtual DOM tree. It then compares this new tree with the old one using a highly optimized Diffing Algorithm.

// A simple React component in 2014
var Timer = React.createClass({
  getInitialState: function() {
    return { seconds: 0 };
  },
  tick: function() {
    this.setState({ seconds: this.state.seconds + 1 });
  },
  componentDidMount: function() {
    this.interval = setInterval(this.tick, 1000);
  },
  render: function() {
    return (
      <div>
        <h1>Seconds Elapsed: {this.state.seconds}</h1>
      </div>
    );
  }
});

When this.setState is called:

  1. React generates a new Virtual DOM tree.
  2. It compares it to the previous one.
  3. It realizes that only the text inside the <h1> has changed.
  4. It issues a single, surgical command to the real DOM: node.textContent = 1;.

The O(n) Diffing Heuristic

Comparing two arbitrary trees is an $O(n^3)$ problem. React's reconciler uses a heuristic that brings this down to $O(n)$. It makes two big assumptions:

  1. Two elements of different types will produce different trees.
  2. The developer can hint at which child elements may be stable across different renders with a key prop.
// Using keys to help the reconciler
render: function() {
  return (
    <ul>
      {this.props.items.map(function(item) {
        return <li key={item.id}>{item.text}</li>;
      })}
    </ul>
  );
}

By providing a key, React doesn't have to guess which <li> is which if the list is reordered. It simply moves the existing DOM nodes around instead of destroying and recreating them.

This declarative model-where we just describe what the UI should look like at any given time-is a massive leap forward. We no longer have to manually track which parts of the DOM need updating. React handles the "how," allowing us to focus on the "what."


Aunimeda builds modern web frontends - from single-page applications to complex multi-locale sites.

Contact us to discuss your frontend project. See also: Web Development, Corporate Website Development

Read Also

Next.js 13/14: Server Actions and the New App Router (2023)aunimeda
Frontend Engineering

Next.js 13/14: Server Actions and the New App Router (2023)

React is coming full circle. In 2023, Next.js Server Actions are bringing back the simplicity of PHP and Ruby on Rails with modern React components.

React Server Components: Decoding the Wire Format (2023)aunimeda
Frontend Engineering

React Server Components: Decoding the Wire Format (2023)

RSC is finally stable in Next.js 13.4. But what's actually happening in that .rsc stream? Let's decode the secret language of the server-client bridge.

Zero-Runtime CSS-in-JS: The Performance King (2023)aunimeda
Frontend Engineering

Zero-Runtime CSS-in-JS: The Performance King (2023)

Is Styled Components dead? In 2023, zero-runtime CSS-in-JS is taking over. No more runtime script, no more style re-calculation.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Web Development

Get Consultation All articles