Virtual List Renders Only Visible Window
Render only the rows inside the scroll viewport and give a spacer the full height, so a 5000-row list paints a dozen nodes per frame.
Code
interface Row {
id: number;
label: string;
}
const rows: Row[] = Array.from({ length: 5000 }, (_, i) => ({
id: i,
label: 'item ' + i,
}));
const ROW_HEIGHT = 32;
const viewport = document.createElement('div');
viewport.style.height = '320px';
viewport.style.overflowY = 'auto';
viewport.style.position = 'relative';
document.body.append(viewport);
// spacer keeps the scrollbar honest about the full data length
const spacer = document.createElement('div');
spacer.style.height = rows.length * ROW_HEIGHT + 'px';
viewport.append(spacer);
function visibleRange(scrollTop: number, viewportHeight: number) {
const first = Math.floor(scrollTop / ROW_HEIGHT);
const count = Math.ceil(viewportHeight / ROW_HEIGHT) + 1;
return { first, last: Math.min(first + count, rows.length) };
}
function render(): void {
const { first, last } = visibleRange(viewport.scrollTop, viewport.clientHeight);
viewport.replaceChildren(spacer);
for (const row of rows.slice(first, last)) {
const el = document.createElement('div');
el.textContent = row.label;
el.style.position = 'absolute';
el.style.top = row.id * ROW_HEIGHT + 'px';
el.style.height = ROW_HEIGHT + 'px';
el.style.width = '100%';
viewport.append(el);
}
}
viewport.addEventListener('scroll', render, { passive: true });
render();
viewport.scrollTop = 64 * ROW_HEIGHT; // simulate the user jumping down
const range = visibleRange(viewport.scrollTop, viewport.clientHeight);
console.log('at scrollTop', viewport.scrollTop, 'rows', range.first, '..', range.last - 1, 'of', rows.length);How the snippet behaves
Virtual List Renders Only Visible Window comes down to the functions visibleRange and render.
Repetition is confined to for (const row of rows.slice(first, last)) — the rest of the file is straight-line.
The result comes back as an object carrying first, count.
Caveats
- A browser is the runtime here:
documentare not available in Node.
Applying it
The primary signature is function visibleRange(scrollTop: number, viewportHeight: number)
The demo output is labeled (at scrollTop); each label names the call that printed it.
The call returns first, count — destructure what you need at the call site.
Statements that matter
viewport.addEventListener('scroll', render, { passive: true });
const rows: Row[] = Array.from({ length: 5000 }, (_, i) => ({
id: i,
label: 'item ' + i,
}));
return { first, last: Math.min(first + count, rows.length) };
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Does `visibleRange` modify its input?
- No. Nothing passed in is written to, which makes the call safe on shared data.
- Does Virtual List Renders Only Visible Window run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.