Virtual DOM
Virtual DOM = JavaScript representation of real DOM
What is Virtual DOM?
"In-memory copy of DOM"
The Virtual DOM is a JavaScript representation of the real DOM that React uses to optimize updates.
"The Virtual DOM is React's in-memory representation of the DOM that enables efficient updates by comparing changes before applying them to the real DOM."
Why Virtual DOM?
Performance optimization
- Real DOM manipulation is expensive
- Virtual DOM is faster to update
- Batch updates efficiently
- Minimize real DOM changes
"The Virtual DOM improves performance by reducing expensive real DOM operations through efficient diffing and batching."
How It Works
Render → Diff → Update
- Render: Create Virtual DOM tree
- State Change: Create new Virtual DOM tree
- Diffing: Compare old vs new (React's algorithm)
- Reconciliation: Update only changed parts in real DOM
"React creates a Virtual DOM tree, compares it with the previous tree when state changes, and updates only the changed parts in the real DOM."
Reconciliation
React's diffing algorithm
React's reconciliation process:
- Compares Virtual DOM trees
- Identifies minimal changes
- Updates real DOM efficiently
- Uses keys for list optimization
"Reconciliation is React's algorithm for efficiently updating the DOM by comparing Virtual DOM trees and applying minimal changes."
Keys
Keys help React identify elements
{
items.map((item) => <div key={item.id}>{item.name}</div>);
}
- Help React identify which items changed
- Should be unique and stable
- Don't use index if list can reorder
"Keys help React efficiently update lists by identifying which items have changed, been added, or removed."
Benefits
Performance + Developer experience
- Performance: Fewer DOM operations
- Predictable: Declarative updates
- Cross-browser: React handles differences
- Easier: Don't manually manipulate DOM
"Virtual DOM provides better performance, predictable updates, cross-browser compatibility, and a simpler development experience."
Virtual DOM vs Real DOM
| Virtual DOM | Real DOM |
|---|---|
| JavaScript objects | Browser API |
| Fast updates | Slow updates |
| Lightweight | Heavyweight |
| React manages | Browser manages |
"Virtual DOM is a lightweight JavaScript representation, while the real DOM is the browser's actual document structure."
Fiber Architecture
Fiber = new reconciliation engine
- React Fiber is the new reconciliation algorithm
- Enables incremental rendering
- Can pause, abort, or reuse work
- Improves performance and enables features like Suspense
"React Fiber is the new reconciliation engine that enables incremental rendering and better performance through work splitting."
9️⃣ When Virtual DOM Updates
State or props change
- Component state changes
- Props change
- Parent re-renders (unless optimized)
- Context value changes
"Virtual DOM updates when component state, props, or context values change, triggering reconciliation."
Optimization
Prevent unnecessary re-renders
React.memofor componentsuseMemofor valuesuseCallbackfor functions- Proper key usage
"Optimize Virtual DOM updates with React.memo, useMemo, useCallback, and proper keys to prevent unnecessary re-renders."
"The Virtual DOM is React's JavaScript representation of the DOM that enables efficient updates. When state changes, React creates a new Virtual DOM tree, compares it with the previous one (reconciliation), and updates only the changed parts in the real DOM. This minimizes expensive DOM operations. Keys help identify list items, and React Fiber is the modern reconciliation engine that enables incremental rendering."
🧠 Ultra-Short Cheat Sheet
JavaScript representation
Diffing algorithm
Reconciliation
Minimal DOM updates
Keys for lists
Fiber architecture
Performance optimization