Sobes.tech
Middle

There is a list of 5000 products on the page, and the page lags heavily. The business asks to do something about it. What would you suggest?

sobes.tech AI

Answer from AI

To optimize a page with a list of 5000 products and eliminate lags, the following approaches can be suggested:

  1. Pagination or lazy loading — load and display products in portions, for example, 50-100 items, loading new ones when scrolling (infinite scroll) or switching pages.

  2. List virtualization (windowing) — display only the elements visible to the user in the DOM, and do not render the rest. This significantly reduces the load on the browser.

  3. Rendering optimization — use memoization of components, avoid unnecessary re-renders, apply React.memo, shouldComponentUpdate, or similar mechanisms.

  4. Data optimization — minimize the size of transmitted data, use compression, caching.

  5. Asynchronous image loading (lazy loading) — load images only when they appear in the viewport.

Example of using virtualization with React and the react-window library:

import { FixedSizeList as List } from 'react-window';

const Row = ({ index, style, data }) => (
  <div style={style}>
    {data[index].name}
  </div>
);

const ProductList = ({ products }) => (
  <List
    height={600}
    itemCount={products.length}
    itemSize={35}
    width={300}
    itemData={products}
  >
    {Row}
  </List>
);

This approach allows efficient handling of large lists without lag.