// --- BEFORE: plain .map() over a large array ---
function PlainList({ items }) {
return (
{items.map((item) => (
-
{item.text}
))}
);
}
// --- AFTER: react-window virtualizes the list ---
// npm install react-window
import { FixedSizeList } from "react-window";
function VirtualizedList({ items }) {
return (
{({ index, style }) => {items[index].text}
}
);
}
function App() {
const items = Array.from({ length: 2000 }, (_, i) => ({ id: i, text: `Item ${i}` }));
// return ; // renders all 2000 elements at once
return ; // only renders the ~12 rows currently visible
}
export default App;
/*
Notes:
- PlainList creates 2000 real DOM elements immediately, even
though only a dozen or so are ever visible in the 400px scroll area
at once — noticeably slower to render and scroll.
- FixedSizeList only renders the handful of rows currently in (or
just outside) the visible viewport, reusing the same small set of
DOM nodes as the user scrolls, regardless of how large itemCount is.
*/