PRO 30% Free • Stripe ($15) & Razorpay (₹899)

React: The Virtual DOM

Top Course Staff · 2 min read · Web development · PRO · 30% Free Preview Available

React: The Virtual DOM

The Problem

DOM manipulation is the heart of the modern, interactive web. Unfortunately, it is also a lot slower than mostJavaScriptoperations.

This slowness is made worse by the fact thatmost JavaScript frameworks update the DOM much more than they have to.

As an example, let’s say that you have a list that contains ten items. You check off the first item. Most JavaScript frameworks would rebuildthe entire list. That’s ten times more work than necessary! Only one item changed, but the remaining nine get rebuilt exactly how they were before.

Rebuilding a list is no big deal to a web browser, but modern websites can use huge amounts of DOM manipulation. Inefficient updating has become a serious problem.

To address this problem, the people at React popularized something called thevirtual DOM.

The Virtual DOM

Here is a refresher of what happens behind the scenes inThe Virtual DOM.

In React, for everyDOM object, there is a corresponding “virtual DOM object.” A virtual DOM object is arepresentationof a DOM object, like a lightweight copy.

A virtual DOM object has the same properties as a real DOM object, but it lacks the real thing’s power to directly change what’s on the screen.

Manipulating the DOM is slow. Manipulating the virtual DOM is much faster because nothing gets drawn onscreen. Think of manipulating the virtual DOM as editing a blueprint, as opposed to moving rooms in an actual house.

How It Helps

When you render a JSX element, every single virtual DOM object gets updated.

This sounds incredibly inefficient, but the cost is insignificant because the virtual DOM can update so quickly.

Once the virtual DOM has been updated, then React compares the virtual DOM with a virtual DOMsnapshotthat was taken right before the update.

By comparing the new virtual DOM with a pre-update version, React figures outexactly which virtual DOM objects have changed.This process is called “diffing.”

Once React knows which virtual DOM objects have changed, then React updates those objects,and only those objects,on the real DOM. In our example from earlier, React would be smart enough to rebuild your one checked-off list-item and leave the rest of your list alone.

This makes a big difference! React can update only the necessary parts of the DOM. React’s reputation for performance comes largely from this innovation.

In summary, here’s what happens when you try to update the DOM in React:

  • The entire virtual DOM gets updated.
  • The virtual DOM gets compared to what it looked like before you updated it. React figures out which objects have changed.
  • The changed objects, and the changed objects only, get updated on therealDOM.
  • Changes on the real DOM cause the screen to change.

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

30% Free Preview Reached · Unlock Remaining 70%

Complete Payment to Unlock Full Guide & Source Code

You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.

✓ Full Access to All 160+ Projects
✓ Instant Blur Removal Everywhere
✓ Production Source Code & Blueprints
✓ All Future Deep Dives Included
Instant Unhindered Activation ·
End of Preview · Unlock Complete Blueprint & Source Code

Ready to access the complete step-by-step tutorial?

Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.

Production AI Blueprints

Ready to build production-grade AI systems?

Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.