Efficiently render large lists by only rendering visible items. Improves performance for thousands of items.
Virtual scrolling only renders visible items, dramatically improving performance for large lists. Try scrolling through thousands of items smoothly!
PERFORMANCE
60 FPS
Smooth scrolling
DOM NODES
~20
vs 1,000 regular
MEMORY
95%
Less memory used
Calculate Visible Range
Determine which items are currently in the viewport based on scroll position
Render Only Visible Items
Only create DOM nodes for items that are visible (plus a small buffer)
Position with Transform
Use CSS transforms to position items correctly without rendering everything
Update on Scroll
Recalculate and re-render as the user scrolls through the list
✓ Large Data Tables
Thousands of rows without lag
✓ Infinite Scroll
Social media feeds and timelines
✓ Chat Applications
Long message histories
✓ File Explorers
Directories with many files
const VirtualList = ({ items, itemHeight }) => {
const [scrollTop, setScrollTop] = useState(0)
const containerHeight = 600
const startIndex = Math.floor(scrollTop / itemHeight)
const endIndex = Math.ceil(
(scrollTop + containerHeight) / itemHeight
)
const visibleItems = items.slice(
startIndex,
endIndex
)
return (
<div
style={{ height: containerHeight, overflow: 'auto' }}
onScroll={(e) => setScrollTop(e.target.scrollTop)}
>
<div style={{ height: items.length * itemHeight }}>
{visibleItems.map((item, i) => (
<div
key={startIndex + i}
style={{
position: 'absolute',
top: (startIndex + i) * itemHeight
}}
>
{item}
</div>
))}
</div>
</div>
)
}