Frontend School

Virtualized Product Catalog with Performance Optimization

Machine Coding
medium

Virtualized Product Catalog with Performance Optimization

Medium
Virtualized List
Performance
React
Frontend
Machine Coding

Overview

In modern web applications, displaying large lists of data efficiently is critical for a smooth user experience. Imagine an e-commerce platform with a product catalog containing thousands, or even tens of thousands, of items. Rendering all these items simultaneously would severely impact performance, leading to slow load times and janky scrolling.

This problem challenges you to build a performant ProductList component that utilizes virtualization techniques to display a large dataset without sacrificing responsiveness. Your solution should only render the items currently visible in the viewport, plus a small buffer, significantly reducing DOM overhead and improving perceived performance.

What you need to do / Task

Your primary task is to implement a ProductList component that efficiently displays a large array of Product objects. Specifically, you need to:

  1. Create a ProductList component: This component will be responsible for rendering the list of products.
  2. Implement Virtualization: Only a subset of ProductItem components should be rendered in the DOM at any given time. This subset should include items visible in the scrollable viewport, plus a small buffer above and below to ensure smooth scrolling.
  3. Display Product Details: Each ProductItem should display the product's name, description, and price.
  4. Ensure Smooth Scrolling: The list should scroll smoothly without noticeable performance degradation, even with a very large number of underlying data items.

UI/UX expectations

  • The ProductList should be contained within a scrollable div or similar element with a fixed height.
  • Each ProductItem must have a consistent, fixed height to simplify virtualization calculations.
  • Basic styling should be applied to make the product items distinct and readable.
  • No complex interactions (e.g., filtering, sorting, item selection) are required beyond scrolling.
  • The list should visually represent a continuous scroll, even though items are dynamically swapped.

Acceptance criteria

  • Only a limited number of ProductItem components are present in the DOM at any given time, regardless of the total number of products in the data prop.
  • Scrolling the ProductList is fluid and responsive, exhibiting no noticeable jank or delays.
  • The correct product information (name, description, price) is displayed for all visible items as the user scrolls.
  • The ProductList component gracefully handles an empty products array, displaying nothing or a placeholder message.
  • The component's overall structure is clear and maintainable, reflecting good React development practices.

Constraints

Constraints & assumptions

  • Assume a fixed height for each product item (e.g., ITEM_HEIGHT = 100px). Your solution should use this constant.
  • The total number of products can be very large (e.g., 10,000 to 50,000 items). The mock data provided will reflect this scale.
  • Data is provided as a static array to the ProductList component. You do not need to implement data fetching or lazy loading.
  • You are expected to use React functional components and hooks.
  • Crucially, you are NOT allowed to use third-party virtualization libraries (e.g., react-window, react-virtualized). You must implement the virtualization logic yourself.

Out of scope

  • Fetching data from an API or implementing infinite scrolling (loading more data when scrolling to the end).
  • Filtering, sorting, searching, or any other data manipulation features.
  • Complex accessibility features beyond basic semantic HTML elements.
  • Responsive design or handling dynamic window resizing.
  • Handling variable item heights.

What interviewers evaluate

  • Correctness of Virtualization Logic: Does the component accurately calculate and render the correct subset of items based on scroll position?
  • Performance Optimization: Is the component designed to minimize re-renders and unnecessary computations, leading to smooth scrolling?
  • Code Quality & Structure: Is the code clean, readable, well-organized, and does it follow modern React best practices?
  • Effective Use of React Hooks: Are useState, useEffect, useRef, useCallback, useMemo (where appropriate) used effectively to manage state, side effects, and optimize performance?

Loading editor...