Skip to content
tezvyn:

List Virtualization: Rendering the Viewport, Not the Dataset

Source: tanstack.comMediumHow cards are made

List Virtualization: Rendering the Viewport, Not the Dataset

List virtualization renders only the visible slice of a huge dataset, keeping your app fast by maintaining a tiny DOM. Use it for long lists or infinite scroll feeds.

Why it exists

Browsers struggle to render and manage thousands of DOM nodes at once. For long lists or data grids, this leads to slow initial loads, high memory usage, and a sluggish UI. List virtualization was created to solve this performance bottleneck by ensuring the number of DOM elements remains small and constant, regardless of the dataset's size.

The mental model

Imagine a list with 10,000 rows. Instead of creating 10,000 DOM elements, you create a tall, empty scrollable container and just a handful of elements—enough to fill the screen plus a small buffer. As the user scrolls, you don't move a giant list; you rapidly recycle those few elements, changing their content and repositioning them with absolute coordinates to create the illusion of a seamless, massive list. You render the viewport, not the entire dataset.

How it works

The process involves four main steps. First, you tell the virtualization library the total count of items in your list. Second, you provide an estimated size for each item so it can calculate the total scroll height immediately, making the scrollbar behave correctly. Third, as items become visible, the library measures their actual rendered size, which is crucial for dynamic content. Finally, your code takes the 'virtual items' provided by the library—a small array of data and positions—and renders them into your own components.

When to use it

Use list virtualization for any UI with a large number of items in a scrollable container. Common examples include data tables with thousands of rows, infinite scroll feeds like a social media timeline, large dropdown menus, and chat applications. It's a must for maintaining a smooth user experience with large datasets.

When not to use it

Avoid virtualization for short lists where the performance overhead of rendering all items is negligible (e.g., fewer than 100 simple items). It adds implementation complexity, so if performance is not an issue, direct rendering is simpler. It's also tricky for layouts where items are not in a single, linear flow, like a masonry grid.

One canonical example

A chat application is a perfect use case. It must handle potentially thousands of messages while remaining performant. Virtualization allows it to render only the visible messages. Modern libraries also support features critical for chat, like 'end anchoring' to keep the view locked to the bottom as new messages arrive, and dynamically measuring message bubbles as their content loads and resizes.

Interview question

What is the fundamental mechanism by which list virtualization improves performance for large datasets?

  • a.It dynamically loads data from the server in small batches as the user scrolls, minimizing initial load.
  • b.It pre-fetches all data and stores it in memory, reducing network requests during scrolling.
  • c.It maintains a small, fixed number of DOM elements, recycling and repositioning them as the user scrolls.Correct
  • d.It renders the entire dataset into a single, highly optimized, non-interactive canvas element.
Why?

List virtualization's core principle is to keep the number of DOM elements small and constant by recycling and repositioning them as the user scrolls, creating the illusion of a large list. Option A describes infinite scrolling or pagination, which is a data loading strategy often paired with virtualization but is not the virtualization technique itself.

Just read this? Test yourself on what you have been reading.

Read the original → tanstack.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles