Virtual DOM is a lightweight copy of the real DOM tree of the page, stored in JavaScript memory.
Simple analogy: Imagine you are renovating a room (real DOM). Instead of moving the cabinet, drilling walls, and painting the ceiling every time, you first draw on paper (Virtual DOM): "This is how it should look after renovation." Then you compare the drawing with the current state of the room and do ONLY what changed: moved the chair, hung a picture. This is fast.
Another vivid analogy: Think of it like a restaurant order system. The real DOM is the actual kitchen — chopping ingredients, firing up stoves, and plating dishes is expensive and time-consuming. The Virtual DOM is the order slip. Instead of rebuilding the entire kitchen for every new order, the chef reads the slip, figures out exactly what changed ("this table needs an extra fork, that table needs the soup removed"), and makes only those adjustments. The kitchen stays efficient.
Without Virtual DOM: Change data → browser redraws the ENTIRE page from scratch. Very slow.
With Virtual DOM:
How the diffing algorithm works step by step:
<div> becomes a <span>, the old element and all its children are destroyed and rebuilt.className changed from "active" to "inactive", only that attribute is updated in the real DOM.Example: You have a list of 1000 tasks. You change one task (to "Completed").
1function TaskList({ tasks }) {2 return (3 <ul>4 {tasks.map(task => (5 <li key={task.id} className={task.done ? "completed" : ""}>6 {task.name}7 </li>8 ))}9 </ul>10 );11}12// When one task.done changes, React only updates that <li>13// The other 999 <li> elements remain untouched
Why it matters:
Common mistakes:
React 16+ uses Fiber — a new reconciliation system that can pause and resume work to avoid slowing down the interface. Fiber breaks rendering work into small units called "fibers" and can prioritize urgent updates (like user input) over less urgent ones (like data fetching results). This is what enables concurrent features like useTransition and useDeferredValue in React 18+.