Skip to content

This project demonstrates the implementation of a virtualized list in JavaScript, optimizing the rendering of large quantities of items.

Notifications You must be signed in to change notification settings

Santosl2/virtualized-list-from-scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Virtualized List Example

This project demonstrates how to implement a virtualized list in JavaScript. Virtualization is a technique used to optimize the performance of large lists by only rendering the items that are visible in the viewport, along with a buffer of items above and below the visible area.

Preview

screen-capture.1.webm

Benefits

List virtualization offers several advantages, including:

  • Improved Performance: By rendering only the visible items, virtualization reduces the initial load time and minimizes the DOM manipulation required during scrolling, resulting in a faster and smoother user experience.

  • Reduced Memory Usage: Virtualization keeps only a limited number of items in the DOM, which reduces memory usage, particularly when dealing with large datasets.

Limitations

While list virtualization provides substantial benefits, it does have some limitations:

  • Complexity: Implementing list virtualization can add complexity to the codebase, especially when dealing with dynamic item sizes, variable data loading, or complex item interactions.

  • Breaks Browsers Ctrl+F Functionality: Since list virtualization keeps most of the items in a virtual state, the browser’s Ctrl+F (Find) functionality cannot directly search for invisible elements. To overcome this, you can implement a custom search component that first searches your data, finds matches, calculates their position, and scrolls to them when requested.

Table of Contents

Getting Started

To get started, simply include the following HTML structure in your project:

<div id="container" style="height: 500px; overflow: auto;">
  <div id="viewport"></div>
</div>

Add the JavaScript code provided in this repository, and customize the itemHeight, bufferSize, and numberOfItems as needed.

How It Works

This implementation creates a virtualized list that efficiently renders only the items that are visible in the viewport, along with a buffer of additional items to provide smooth scrolling. The key idea is to calculate the start and end indices of the items to be rendered based on the current scroll position and then adjust the position of the rendered items using a translateY transformation.

Code Explanation

Element Selection

const container = document.getElementById("container");
const viewport = document.getElementById("viewport");
  • container: The element that contains the scrollable list.
  • viewport: The element where the list items will be rendered.

Configuration Variables

const itemHeight = 50; // Height of each item
const bufferSize = 20; // Number of items to render above and below the visible area
const numberOfItems = 1000; // Total number of items
  • itemHeight: The height of each item in the list (in pixels).
  • bufferSize: The number of items to render above and below the visible area to provide smooth scrolling.
  • numberOfItems: The total number of items in the list.

Rendering Function

function renderItems(startIndex, endIndex) {
  viewport.innerHTML = ""; // Clear the viewport
  for (let i = startIndex; i < endIndex; i++) {
    const item = document.createElement("div");
    item.className = "item";
    item.textContent = `Item ${i + 1}`;
    viewport.appendChild(item);
  }
  viewport.style.transform = `translateY(${startIndex * itemHeight}px)`;
}
  • Clearing the Viewport: The viewport is cleared before rendering new items.
  • Creating Items: Items are dynamically created and added to the viewport.
  • Position Adjustment: The translateY transformation adjusts the vertical position of the viewport based on the startIndex.

Scroll Event Handler

function onScroll(e) {
  const scrollTop = container.scrollTop;
  const startIndex = Math.max(
    Math.floor(scrollTop / itemHeight) - bufferSize,
    0
  );
  const endIndex = Math.min(
    startIndex +
      bufferSize * 2 +
      Math.ceil(container.clientHeight / itemHeight),
    numberOfItems
  );

  renderItems(startIndex, endIndex);
}
  • Calculating startIndex: The index of the first item to render is based on the scroll position and the item height.
  • Calculating endIndex: The index of the last item to render is based on the visible area and buffer size.
  • Rendering Items: Calls renderItems with the calculated indices.

Initialization

container.addEventListener("scroll", onScroll);
onScroll();
  • Scroll Listener: The onScroll function is called whenever the container is scrolled.
  • Initial Rendering: The onScroll function is called once during initialization to render the initial set of items.

Mathematical Rules

  • Start Index Calculation:

    • Math.floor(scrollTop / itemHeight): Converts the scroll position into an item index.
    • - bufferSize: Subtracts the buffer size to render additional items above the visible area.
    • Math.max(..., 0): Ensures the start index is not negative.
  • End Index Calculation:

    • startIndex + bufferSize * 2 + Math.ceil(container.clientHeight / itemHeight): Calculates the end index based on the visible area and buffer size.
    • Math.min(..., numberOfItems): Ensures the end index does not exceed the total number of items.
  • translateY Transformation: Adjusts the position of the rendered items based on the startIndex multiplied by the item height.

Conclusion

This virtualized list implementation is an efficient way to handle large lists in web applications, reducing the rendering load by only displaying the items that are visible in the viewport. By understanding the key concepts and mathematical rules, you can customize and extend this implementation to suit your needs.

About

This project demonstrates the implementation of a virtualized list in JavaScript, optimizing the rendering of large quantities of items.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published