List Virtualization in React

Atul Banwar
4 min readAug 4, 2023

--

Optimizing Performance for Long List

List Virtualization Example

Have you ever noticed that when you render a long list of items on your UI, your app starts to feel sluggish? This happens because your web page is dealing with a large number of elements, many of which are not even visible until the user scrolls. What if there was a way to avoid rendering those invisible items until they are needed? That’s where list virtualization comes to the rescue!

Understanding and How It Works

List virtualization is a smart technique used to optimize the rendering of large lists by only displaying the items that are currently visible on the screen. Instead of rendering all the items at once, list virtualization keeps the majority of them in a virtual state, meaning they are not actually present in the DOM.

Here’s how it works:

Initial Render: When your app starts, list virtualization only renders the visible items that fit within the container’s viewable area in the DOM.

Scroll Event Handling: As the user scrolls through the list, a scroll event is triggered. The virtualization library listens to this event and calculates which items should be visible based on the scroll position.

Dynamic Rendering: As you scroll, the virtualization library dynamically renders the new visible items while removing off-screen ones. This reduces the amount of rendering required and ensures a smooth and efficient scrolling experience.

By using list virtualization, your app can handle even huge lists of thousands or millions of items without slowing down. It’s like magic, making your app feel faster and more responsive to users.

Existing Solutions for List Virtualization in React

Some of the popular libraries for implementing list virtualization in React applications are:

react-window: A powerful and lightweight virtualization library for rendering large lists and tabular data. It is a rewrite of react-virtualized, making it smaller and faster. It provides components like FixedSizeList, VariableSizeList, and FixedSizeGrid, catering to different list types and scenarios. The library is highly optimized for performance and has minimal dependencies.

react-virtualized: This widely adopted virtualization library has a rich set of components for efficiently rendering large lists and tables. It includes components like VirtualizedList, VirtualizedGrid, and Masonry, offering flexibility and customization options.

react-virtuoso: Designed specifically for virtualizing variable-sized lists with dynamic item heights, react-virtuoso focuses on providing smooth scrolling experiences for lists with items of varying heights. It includes a flat list, a grouped list, a table, and grid components.

A Simple Example with react-window

Let’s see a basic example of how to use react-window to virtualize a list of items in a React component:

import React from "react";
import { FixedSizeList } from "react-window";

const data = Array.from({ length: 100000 }, (_, index) => `Item ${index}`);

const renderRow = ({ index, style }) => (
<div style={{ ...style, display: "flex", alignItems: "center", borderBottom: "1px solid lightgrey" }}>
<span>{data[index]}</span>
</div>
);

const VirtualizedListExample = () => (
<div style={{ height: "400px", width: "300px", border: "1px solid lightgrey" }}>
<FixedSizeList
height={400}
width={300}
itemCount={data.length}
itemSize={40} // Height of each row
>
{renderRow}
</FixedSizeList>
</div>
);

export default VirtualizedListExample;

In this example, we use the FixedSizeList component from react-window to build a list with 100,000 items. The renderRow function defines how each item is rendered, while the FixedSizeList efficiently handles rendering only the visible items on the screen, ensuring optimal performance with such a large list of items.

Benefits

List virtualization offers several advantages, including:

Improved Performance: By rendering only the visible items, virtualization reduces the initial load time and minimizes the DOM manipulation required during scrolling, resulting in a faster and smoother user experience.

Reduced Memory Usage: Virtualization keeps only a limited number of items in the DOM, which reduces memory usage, particularly when dealing with large datasets.

Limitations

While list virtualization provides substantial benefits, it does have some limitations:

Complexity: Implementing list virtualization can add complexity to the codebase, especially when dealing with dynamic item sizes, variable data loading, or complex item interactions.

Breaks Browsers Ctrl+F Functionality: Since list virtualization keeps most of the items in a virtual state, the browser’s Ctrl+F (Find) functionality cannot directly search for invisible elements. To overcome this, you can implement a custom search component that first searches your data, finds matches, calculates their position, and scrolls to them when requested.

Conclusion

List virtualization is a crucial optimization technique for React applications dealing with large lists of data. By rendering only the visible items and dynamically replacing off-screen items, list virtualization significantly improves performance, leading to faster load times and smoother scrolling experiences.

--

--