Working on a task management UI at Wokay, I had to render 1000+ interactive rows in a spreadsheet-like layout. Scroll performance was terrible. So I reached for virtualization.
The basic idea
Virtualization means only rendering what's visible. You calculate the visible portion of the screen using scroll offset, item size, and viewport height. Then you render just those items.
It works like a stage. Only the actors in the spotlight are out there, and everyone else waits backstage.
// Simplified concept
function VirtualList({ items, itemHeight, viewportHeight }) {
const [scrollTop, setScrollTop] = useState(0);
const startIndex = Math.floor(scrollTop / itemHeight);
const endIndex = Math.min(
startIndex + Math.ceil(viewportHeight / itemHeight) + 1,
items.length
);
const visibleItems = items.slice(startIndex, endIndex);
return (
<div
style={{ height: viewportHeight, overflow: 'auto' }}
onScroll={(e)=> setScrollTop(e.currentTarget.scrollTop)}
>
<div style={{ height: items.length * itemHeight }}>
{visibleItems.map((item, i) => (
<div
key={startIndex + i}
style={{
position: 'absolute',
top: (startIndex + i) * itemHeight,
height: itemHeight,
}}
>
{item.name}
</div>
))}
</div>
</div>
);
}
Where I went wrong
We chose virtua for our virtualization library. It's lightweight and has a great API, and scroll performance looked great at first. But I had written this:
// This still creates JSX for ALL 1000 items
<Virtualizer>
{tasks.map(task => <TaskRow task={task} />)}
</Virtualizer>
React still builds the JSX for all 1000 items. Virtua only puts 20 in the DOM, but React has already created elements for all of them.
The fix
Only pass the visible items to the virtualizer, or restructure so the mapping happens inside the virtualization boundary:
// Better: let the virtualizer control what gets mapped
<VirtualList count={tasks.length}>
{(index) => <TaskRow task={tasks[index]} />}
</VirtualList>
This way, the render function only runs for visible indices.
What I learned
- Virtualizing the DOM is not enough if you still create JSX for every item
React.memohelps but only if JSX isn't being rebuilt- Move expensive logic outside render or defer it
- Smooth scrolling alone proves little, so check with the React DevTools Profiler