惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
罗磊的独立博客
C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
B
Blog RSS Feed

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How React Virtual DOM Works Under the Hood: A Hilarious J...
Bhupesh Chan · 2026-05-09 · via DEV Community

Imagine you’re redecorating your living room. Every time you want to move a cushion, you demolish the entire house, rebuild it from scratch, and then place the cushion exactly where it was. Sounds efficient, right? Welcome to manual DOM manipulation in the olden days. React’s Virtual DOM is the smart friend who says, “Bro, just move the cushion.”

Let’s dive into how this magic actually works — in a brain-friendly, memorable, and slightly unhinged way.

1. The Problem Virtual DOM Solves

The Real DOM (Document Object Model) is like a heavy, wooden Victorian mansion. It’s beautiful but extremely expensive to renovate.

  • Every tiny change (adding a <p> tag, updating text, changing a class) triggers:
    • Layout recalculation (reflow)
    • Repaint
    • Possibly style recalculation for the whole page

Do this 1000 times per second (hello, dynamic UIs, animations, lists) and your app becomes slower than a sloth on sedatives.

Direct DOM updates = Performance suicide.

2. Real DOM vs Virtual DOM (The Buddy Cop Duo)

Aspect Real DOM Virtual DOM
Nature Heavyweight HTML tree in browser Lightweight JavaScript object tree
Update Cost Very expensive Cheap (just JS objects)
Mutation Direct & slow Immutable-style (new tree created)
Personality Grumpy grandpa Chill JavaScript bro

Virtual DOM is simply a plain JavaScript representation of your UI. Something like this in memory:

{
  type: 'div',
  props: { className: 'app' },
  children: [
    { type: 'h1', props: {}, children: ['Hello'] }
  ]
}

Enter fullscreen mode Exit fullscreen mode

It’s not the actual DOM — it’s a blueprint.

3. Initial Render Process (The First Date)

  1. You write a component (functional or class).
  2. React calls it → gets JSX.
  3. JSX is transpiled to React.createElement() calls.
  4. This builds the Virtual DOM tree.
  5. ReactDOM takes this tree and creates the actual DOM nodes (this is the only time it touches the Real DOM heavily).
  6. Browser paints it. Done.

Visual Flow:

Your Component → JSX → Virtual DOM Tree → Real DOM (painted)

Enter fullscreen mode Exit fullscreen mode

4. State or Props Change = Drama Time

You call setState() or update props.

React doesn’t immediately touch the Real DOM like a panicked developer. Instead:

  • It schedules a re-render.
  • Your component (and its children) run again.
  • A brand new Virtual DOM tree is created.

Important: The old Virtual DOM tree is still hanging around for comparison.

5. Diffing (Reconciliation) — The Detective Work

This is where React earns its salary.

React compares the old Virtual DOM with the new Virtual DOM (this process is called reconciliation).

It asks smart questions:

  • Do the elements have the same type? (div vs div = good)
  • Do they have the same key (in lists)?
  • Have props changed?

React uses a heuristic algorithm (not a perfect deepest-diff because that would be too slow). It assumes:

  • If two elements of different types appear at the same level → destroy and recreate the whole subtree.
  • Lists need key props for efficient tracking.

Funny mental model: Imagine two identical twins (old tree and new tree) standing next to each other. React is the detective going, “Same nose? Same shirt? Only the left sock changed? Cool, just update the sock.”

6. Minimal Updates to Real DOM (The Magic)

After diffing, React generates a list of minimal changes (the "patch").

Then, in the commit phase, it applies only those changes to the Real DOM.

Examples of minimal updates:

  • Change textContent of one <span>
  • Add a CSS class
  • Insert one new DOM node

Everything else stays untouched.

7. The Full React Render → Diff → Commit Flow

graph TD
    A[State/Props Change] --> B[Render Phase]
    B --> C[New Virtual DOM Tree Created]
    C --> D[Reconciliation / Diffing]
    D --> E[Commit Phase]
    E --> F[Minimal Real DOM Updates]

Enter fullscreen mode Exit fullscreen mode

Render Phase: Creates new Virtual DOM (can be paused/cancelled in modern React — Fiber).
Commit Phase: Synchronous — actual DOM mutations happen here.

Why This Approach is Genius for Performance

  • Creating JS objects is way cheaper than touching the Real DOM.
  • Diffing is done in memory at JavaScript speed.
  • Only the necessary mutations reach the browser.
  • Batch updates: Multiple setState calls in one event handler → one re-render.

Result? Smooth 60fps UIs even with complex interfaces.

Memorable Analogy (Never Forget This)

Think of your UI as a theater stage:

  • Real DOM = Actual actors and props on stage. Moving them is slow and noisy.
  • Virtual DOM = The script + lighting diagram in the director’s notebook. You can rewrite the entire script instantly.
  • Diffing = Director comparing old script vs new script and only telling actors what actually changed.
  • Commit = Only the necessary actors move. The audience barely notices the change.

Final Words

The Virtual DOM isn’t magic — it’s a brilliant engineering tradeoff. It sacrifices a little memory (keeping two trees temporarily) to save massive amounts of expensive DOM operations.

Next time someone says “React is slow,” gently remind them that without Virtual DOM, their fancy interactive dashboard would feel like it was built in 2005 with jQuery plugins.

Now go build something buttery smooth.


Bonus Tip for Interviews:

When asked “How does Virtual DOM work?”, don’t say “It makes things fast.” Say:

“React maintains a lightweight JS representation, diffs it with the previous version using heuristics, and surgically updates only the changed nodes in the Real DOM during the commit phase.”

You’ll sound smart. And you’ll remember it forever because of the grumpy grandpa and theater director analogies.

Happy coding, you magnificent React wizard! 🧙‍♂️