What Virtualization Is: Render Only Visible Items
You'll learn how virtualization solves performance problems by rendering only what users can actually see.
The Core Concept
Virtualization is a technique where you render only the items currently visible in the viewport, plus a small buffer zone above and below. Instead of creating 10,000 DOM elements for a 10,000-item list, you might only render 20—just enough to fill the screen.
Think of it like a theater stage with a spotlight. You have thousands of actors waiting backstage, but only the ones in the spotlight need costumes and makeup. As the spotlight moves, different actors get ready while others step back. The audience never knows the difference.
How It Works
When you scroll through a virtualized list:
- Calculate visible range: Determine which items are in or near the viewport based on scroll position and item height
- Render only those items: Create DOM elements for maybe 15-30 items instead of thousands
- Position with transforms: Use CSS positioning to place visible items at their correct scroll position
- Maintain scroll height: Create a container with the total height (as if all items were rendered) so scrollbars work correctly
- Update on scroll: As you scroll, new items render into view and old ones unmount
The Performance Win
Instead of React reconciling 10,000 list items on every state change, it only reconciles the ~20 that are rendered. The browser only paints ~20 DOM nodes instead of 10,000. Memory usage drops dramatically. Scroll performance stays smooth even with massive datasets.
The trade-off is complexity: you need to calculate positions, handle variable heights carefully, and ensure keyboard navigation still works.
Key Takeaway: Virtualization renders only visible items plus a buffer, dramatically improving performance for long lists by keeping the DOM small regardless of total data size.