Member-only story
Efficiently rendering large lists: An in-depth look at virtual scrolling and other performance optimization techniques
4 min readJan 25, 2023
Virtual scrolling is a technique that allows for efficient rendering of large lists by only rendering the items that are currently visible on the screen. This is done by calculating the start and end indices of the data that should be visible based on the scroll position, and then only rendering the items within that range. This can greatly improve performance, especially when dealing with large data sets or complex items that take a long time to render.
A basic virtual scroll component implemented in React could look something like this:
import React, { useEffect, useRef, useState } from "react";
const VirtualScroll = ({ data, itemHeight, renderItem }) => {
const dataSize = data?.length ?? 0;
const [start, setStart] = useState(0);
const [end, setEnd] = useState(dataSize);
const [visibleData, setVisibleData] = useState([]);
const listRef = useRef(null);
const buffer = 3; // number of extra items to render…