Sub topic 15
When not to virtualize: pagination and content-visibility
This chapter assumes you can build the fixed-height virtual list from earlier chapters. You should also know React state and basic CSS.
Virtualization has a price
A virtual list removes most rows from the DOM. That is also its cost:
- Browser find-in-page cannot see rows that are not rendered.
- Screen readers get an incomplete list unless you add attributes such as
aria-setsizeandaria-posinset. - You must handle row heights, scroll position, keys, and focus yourself or through a library.
- Bugs show up as blank gaps, jumping scrollbars, and lost row state.
If your list has a few hundred simple rows, this code and complexity may not be worth it. Measure first. If rendering 500 rows takes a few milliseconds, leave the list alone.
Option 1: pagination
Pagination shows a fixed number of rows per page, so the DOM never grows beyond that number. It also gives users stable positions, shareable URLs, and a working find-in-page within the current page. It suits data that people browse in steps, such as search results or admin tables.
import { useState } from "react";
const PAGE_SIZE = 50;
export function PagedList({ items }: { items: string[] }) {
const [page, setPage] = useState(0);
const pageCount = Math.max(1, Math.ceil(items.length / PAGE_SIZE));
const start = page * PAGE_SIZE;
const visible = items.slice(start, start + PAGE_SIZE);
return (
<div>
<ul>
{visible.map((item, i) => (
<li key={start + i}>{item}</li>
))}
</ul>
<button onClick={() => setPage((p) => p - 1)} disabled={page === 0}>
Previous
</button>
<span>
{" "}Page {page + 1} of {pageCount}{" "}
</span>
<button
onClick={() => setPage((p) => p + 1)}
disabled={page >= pageCount - 1}
>
Next
</button>
</div>
);
}The trade-off is that users must click to move through data. Pagination is a poor fit for feeds or chat logs, where continuous scrolling is expected.
Option 2: CSS content-visibility
The CSS property content-visibility: auto tells the browser to skip layout and painting for elements that are off screen. The elements stay in the DOM, so find-in-page and the accessibility tree keep working.
.row {
content-visibility: auto;
contain-intrinsic-size: auto 40px;
}contain-intrinsic-size gives skipped rows a placeholder height. The auto keyword makes the browser remember each row's real height once it has rendered. Without a placeholder, skipped rows collapse to zero height and the scrollbar becomes unreliable.
Choosing between them
| Situation | Better choice |
|---|---|
| Hundreds of rows, slow layout or paint | `content-visibility: auto` |
| Data browsed in steps, needs URLs per page | Pagination |
| Thousands of rows, continuous scrolling, slow React render | Virtualization |
| Users rely on find-in-page across the whole list | Pagination or `content-visibility` |
| Fewer than about 200 simple rows | Plain list |
You can combine approaches. A paginated table with 200 rows per page can use content-visibility to stay fast without a virtualizer.
Common mistakes
- Virtualizing before profiling, then spending time on a problem that did not exist.
- Using
content-visibilitywithoutcontain-intrinsic-size, which causes scrollbar jumps. - Expecting
content-visibilityto fix slow component renders. - Paginating with array indexes as keys when items can be inserted or removed. Use stable IDs when you have them.
Exercise
A page renders 800 comment rows. Profiling shows React's render takes 8 ms, but browser layout and paint take 400 ms. Users also need Ctrl+F to search all comments. Which technique do you pick, and why?
Show answer
Use content-visibility: auto with contain-intrinsic-size: auto <estimated height> on each row. The bottleneck is browser layout and paint, which this property skips for off-screen rows. All rows stay in the DOM, so find-in-page still works. Virtualization would hide most rows from Ctrl+F and add complexity without reducing the cost that matters here.