What You'll Learn
- 1Estimate when a list becomes a performance problem and what exactly degrades
- 2Render only visible rows while keeping scroll height and position correct
- 3Handle variable and dynamic row heights with measurement rather than guesses
- 4Recognise what windowing breaks for search, accessibility and printing
A list of two hundred rows is fine and a list of twenty thousand is not, but the failure is rarely the rendering you would guess. It is not React reconciliation that collapses first; it is the browser. Twenty thousand rows mean tens of thousands of nodes for layout to compute, for style to resolve and for accessibility to walk, and once that exceeds a few hundred thousand nodes every scroll frame triggers work on the main thread that competes with your input handlers. Windowing is the answer: render the rows near the viewport, position them inside a spacer that carries the full scroll height, and keep the DOM at a size the browser can handle regardless of how large the data is.
What actually degrades, and when
The useful skill is recognising the shape of the problem before reaching for a tool. Startup cost grows with the number of nodes created at once, scroll smoothness degrades when style and layout run per frame, and memory grows with the objects React keeps for each element. Each has a different threshold, and a list can be slow for one reason while looking trivially small.
| Row count | Typical behaviour | First thing to try |
|---|---|---|
| Up to about 200 | No measurable cost | Nothing; render them all |
| 200 to 1000 | Slower first paint, fine while scrolling | Cheaper rows, memoised components |
| 1000 to 10000 | Visible scroll stutter on mid-range devices | Windowing with fixed heights |
| Beyond 10000 | Layout and memory dominate, interaction lags | Windowing plus data paging |
| Variable-height rows in any range | Measurement on every scroll frame | Dynamic measurement with caching |
Also check that the list is the culprit. An unpaged table that also runs a filter over an array on every keystroke, inside a component that re-renders because a parent holds unrelated state, will be slow at three hundred rows. Measure first: record the frame time while scrolling, look at the node count in the elements panel, and confirm that the cost scales with row count rather than with something you can fix with a memo.
Fixed heights: the simple window
When every row is the same height the arithmetic is trivial, and a library can compute which indices are visible from the scroll offset alone. The row renderer receives an index and a positioning style, and it must apply that style exactly â the absolute offset is what keeps rows aligned with the spacer, and a row that ignores it lands in the wrong place the moment you scroll.
import { useCallback, useRef } from 'react';
import { FixedSizeList } from 'react-window';
function Row({ index, style, data }) {
const person = data.people[index];
return (
<div
style={style}
className={data.selectedIndex === index ? 'row row-selected' : 'row'}
role="row"
aria-rowindex={index + 1}
>
<span>{person.name}</span>
<span>{person.email}</span>
</div>
);
}
export function PeopleWindow({ people, selectedIndex, onSelect }) {
const listRef = useRef(null);
const rowProps = useCallback(
({ index, style }) => (
<Row index={index} style={style} data={{ people, selectedIndex }} />
),
[people, selectedIndex]
);
return (
<FixedSizeList
ref={listRef}
height={480}
itemCount={people.length}
itemSize={56}
width="100%"
itemData={{ people, selectedIndex }}
overscanCount={6}
>
{rowProps}
</FixedSizeList>
);
}
Overscan is the buffer of extra rows rendered above and below the viewport. Zero gives the smallest DOM and produces visible blank space on fast scrolls or when a row image loads late; six to ten is the usual compromise. If rows contain images that pop in, reserve their space with an aspect ratio so the heights never change after the fact.
Variable heights and measurement you can trust
Real lists have variable heights: an expanded row, a wrapping comment, a preview thumbnail. The naive approach is an estimate function based on index, which works until the estimate is wrong and the scrollbar starts jumping as the library corrects itself. The reliable approach is to measure the rows that have actually rendered, cache those heights by index, and treat the estimate only for rows that have never been seen.
import { useRef } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
export function DynamicFeed({ items }) {
const parentRef = useRef(null);
const virtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 72,
overscan: 8,
getItemKey: (index) => items[index].id,
});
return (
<div ref={parentRef} className="feed" style={{ height: 520, overflow: 'auto' }}>
<div
className="feed-inner"
style={{ height: virtualizer.getTotalSize(), position: 'relative' }}
>
{virtualizer.getVirtualItems().map((virtualRow) => (
<div
key={virtualRow.key}
data-index={virtualRow.index}
ref={virtualizer.measureElement}
className="feed-item"
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: 'translateY(' + virtualRow.start + 'px)',
}}
>
<h3>{items[virtualRow.index].title}</h3>
<p>{items[virtualRow.index].body}</p>
</div>
))}
</div>
</div>
);
}
The measureElement ref is what makes this correct: each row reports its own height after layout, the cache updates, and totals are recomputed. Two habits keep it smooth. Keys must be stable identifiers rather than indices, otherwise a prepend at the top invalidates every cached height and the scroll position shifts under the user. And expansion state should change a row's measured height through the same path, so a row that opens never needs a manual offset calculation elsewhere.
Measurement has a cost, and it appears as a loop if you are careless. Reading a height forces layout, writing a style invalidates it, and a component that reads and writes during the same commit can thrash. Batching measurement through the virtualizer's own callback is why the ref approach is safer than an effect that measures every child on every render.
Loading more, and getting back to where the user was
Infinite loading composes naturally: an item near the end of the window triggers the next page fetch, and the spacer grows when data arrives. What needs care is the illusion of progress. If the fetch returns nothing because the page was empty, the trigger fires again immediately and loops â guard it with an explicit hasMore flag rather than inferring from position.
Scroll restoration is the other half. A user who opens a row and navigates back expects the list at the same offset with the same expansion state, which means the scroll offset and the meaningful selection belong in the URL or in a store that survives the route change. With windowing you must restore after the data for that offset has arrived, because restoring earlier computes a position against a shorter spacer and lands somewhere else entirely.
Jump-to-index uses the same machinery: ask the virtualizer for the offset of an index, set the scroll position, then let measurement settle. Doing it before the data exists is the same mistake as restoring early.
Grids, columns and horizontal windows
Two-dimensional lists change the arithmetic but not the idea. A grid computes rows and columns from an item count and a column count, windows over rows first because that is where the volume is, and positions each cell with a transform. Column count usually depends on container width, so the grid needs to re-derive on resize, and the correct moment to do that is after layout rather than during render.
Horizontal windows are rarer and trickier because the browser natively supports horizontal scrolling with less friction, and many users trackpad-scroll sideways without any virtualization. Apply the same rules if you do: spacer width instead of spacer height, and measurement along the horizontal axis.
What windowing breaks
This is the part teams discover after shipping. Browser find-in-page cannot find text that is not in the document, so a windowed log viewer silently loses a feature users rely on. Screen readers announce the number of items in the accessibility tree, which is now the number of rendered rows, so a list that claims twelve items instead of eight thousand is confusing without proper aria-rowcount. Focus on a row can be lost when its node is unmounted while the user is interacting with it, which matters for keyboard navigation and for any popover anchored to a row.
Print suffers for the same reason as search: only visible rows exist. If printing is a real requirement, render a separate unwindowed view for print media or export to a document instead. And keyboard navigation needs explicit handling â arrow keys must move the selection and scroll the virtualizer to the selected index, because scrolling to a focused element does nothing when that element has no position until it is rendered.
The honest summary is that windowing trades DOM size for edge-case complexity. For a log or a data grid where scanning matters, that trade is clearly worth it. For a list of twenty items with rich interactions, it is a net loss, and the right answer is to leave it alone.
Common Mistakes
Applying windowing to a list of a few dozen rows because a library was already installed
Fix: Measure first and virtualise only when row count or row cost actually dominates
Using array indices as keys so every insert invalidates every cached height
Fix: Key rows by stable record ids so measurements and scroll positions survive inserts
Restoring scroll position before the data for that offset has loaded
Fix: Wait for the page that contains the offset, then restore, then let measurement settle
Rendering an unbounded list of extra rows through a huge overscan value
Fix: Keep overscan between six and ten and reserve space for images instead
Best Practices
- Reserve space for media so row heights never change after first paint
- Keep the windowed DOM small and memoise rows so parent renders do not cascade
- Handle keyboard selection and scrolling explicitly rather than relying on focus
- Announce the true item count with aria-rowcount so assistive technology is not misled
- Keep scroll offset and selection in the URL so back navigation restores the view
Key Takeaways
- Windowing fixes DOM size, not algorithmic cost â filter and sort still scale with the full data
- Fixed heights give trivial arithmetic; variable heights need measurement with stable keys
- Scroll restoration and jump-to-index must wait for the data that defines the offset
- Search, printing, focus and screen reader counts all need deliberate compensation
- A list of two hundred rows usually needs memoisation, not a virtualizer