Virtualisation (windowing) renders only the rows in or near the viewport, plus a spacer that gives the scroll container its full height. That keeps the DOM at a few dozen nodes instead of tens of thousands, which cuts memory, style and layout cost, and INP. The price is complexity: variable row heights, scroll anchoring, keyboard and screen-reader navigation, browser find-in-page, printing and SEO all get harder. Use it when you have a few thousand complex rows. Below that, pagination or content-visibility: auto is often enough.
scrollTop = 12,000px, rowHeight = 40px, viewport = 600px
first = floor(12000 / 40) = 300, visible = ceil(600 / 40) = 15
render rows 295..320 (overscan 5), each translated to index * 40px
spacer height = totalRows * 40px
function getWindow(scrollTop, viewportH, rowH, total, overscan = 5) {
const start = Math.max(0, Math.floor(scrollTop / rowH) - overscan);
const end = Math.min(total, Math.ceil((scrollTop + viewportH) / rowH) + overscan);
return { start, end, offsetY: start * rowH, totalHeight: total * rowH };
}
Libraries such as TanStack Virtual and react-window handle the hard parts: measuring variable-height rows with ResizeObserver, keeping a prefix-sum of heights for O(log n) offset lookups, keeping the scroll position stable when rows above the viewport change height, horizontal and grid virtualisation, and reverse lists (chat).
| Situation | Approach |
|---|---|
| < ~300 simple rows | Plain rendering is fine; measure first |
| Long article-like page with sections | content-visibility: auto + contain-intrinsic-size |
| Thousands of rows, interactive cells (data grid, logs, feed) | Virtualise |
| User needs to scan or compare a bounded set | Pagination, which is also better for shareable URLs |
| Infinite feed | Virtualise + cursor pagination + cap the retained pages |
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 320px; /* remembers real size once rendered */
}
content-visibility: auto skips rendering work for off-screen elements but keeps them in the DOM, so find-in-page, anchors and the accessibility tree still work. It doesn't reduce DOM node count or JS memory, though. It is Baseline in all major browsers since 2024.
role="grid" or a list with aria-setsize and aria-posinset) so the total count and position are announced, and manage focus so keyboard navigation scrolls the next row into existence before focusing it.aria-setsize/aria-posinset, focus management) and the find-in-page trade-off.content-visibility as a lighter-weight alternative for document-style pages.Spotted a mistake or a rights concern? Report an issue