Hone logo
Hone
Problems

Virtual Scroll List Implementation in JavaScript

Creating virtual scroll lists is a common optimization technique for displaying large datasets in web applications. Instead of rendering all items at once, a virtual scroll list only renders the items currently visible in the viewport, significantly improving performance and reducing memory usage. This challenge asks you to implement a basic virtual scroll list component in JavaScript.

Problem Description

You are tasked with creating a JavaScript component that simulates a virtual scroll list. The component should take an array of data as input and render only the items that are currently visible within a specified container element. As the user scrolls, the component should dynamically update the rendered items to reflect the visible portion of the data array.

Key Requirements:

  • Data Input: The component should accept an array of data items as input. Each item can be any JavaScript data type (e.g., strings, numbers, objects).
  • Container Element: The component should require a DOM element as a container where the rendered items will be appended.
  • Viewport Height: The component should be configurable with a viewport height (in pixels). This determines the visible area of the scroll list.
  • Scrolling: The component should listen for scroll events on the container element and update the rendered items accordingly.
  • Dynamic Rendering: Only the items that are within the viewport's range (based on the scroll position) should be rendered.
  • Item Rendering: You are responsible for rendering each item as a simple <div> element with the item's value as its text content. (e.g., <div>${item}</div>).
  • Performance: The component should be optimized to minimize DOM manipulations.

Expected Behavior:

  • Initially, only the items visible within the viewport should be rendered.
  • As the user scrolls down, new items should be dynamically added to the bottom of the container.
  • As the user scrolls up, items that are no longer visible should be removed from the container.
  • The component should handle scrolling beyond the bounds of the data array gracefully (e.g., by not rendering items that don't exist).

Edge Cases to Consider:

  • Empty data array: The component should not render anything if the input data array is empty.
  • Viewport height larger than data array length: The component should render all items if the viewport is large enough to display the entire data array.
  • Scrolling speed: The component should handle rapid scrolling without performance issues.
  • Data array with varying item sizes: While this challenge focuses on uniform item sizes, consider how your solution might be adapted for varying item heights.

Examples

Example 1:

Input: data = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10"], viewportHeight = 100, container = <div id="scroll-container"></div>
Output:  Only "Item 1" and "Item 2" are rendered inside the container initially.  Scrolling down reveals "Item 3", "Item 4", etc.
Explanation:  With a viewport height of 100 and assuming each item takes up 20 pixels, only the first 5 items are initially visible.  Scrolling reveals more items as the scroll position changes.

Example 2:

Input: data = [], viewportHeight = 200, container = <div id="scroll-container"></div>
Output: The container remains empty.
Explanation:  An empty data array results in no items being rendered.

Example 3:

Input: data = ["Item 1", "Item 2"], viewportHeight = 500, container = <div id="scroll-container"></div>
Output: "Item 1" and "Item 2" are rendered inside the container.
Explanation: The viewport height is larger than the data array length, so all items are rendered.

Constraints

  • Data Array Size: The data array can contain up to 1000 items.
  • Viewport Height: The viewport height will be a positive integer between 50 and 500 pixels.
  • Item Height: Assume each item has a uniform height of 20 pixels.
  • Performance: The component should render updates within 100ms of a scroll event. Excessive DOM manipulations should be avoided.

Notes

  • You can use standard JavaScript DOM manipulation techniques.
  • Focus on the core logic of virtual scrolling – dynamically rendering items based on the scroll position. Styling is not required.
  • Consider using requestAnimationFrame to optimize DOM updates and prevent performance bottlenecks.
  • Think about how to efficiently calculate which items are currently visible within the viewport. The scroll position and viewport height are your key inputs.
  • The container element should already exist in the DOM before the component is initialized.
Loading editor...
javascript