Guides

Performance

Keep scrolling responsive and cut the requests a network-backed grid sends.

This guide covers the changes that keep scrolling responsive, in order of impact. The grid does little on a scroll event. If the first visible row has not changed, it compares two numbers and stops, and it recomputes the rendered window only when that row changes. The benchmarks show that this work is a small fraction of a frame. What slows a grid down is the code you add around it.

The samples assume the grid from Installation, including its length and pageProvider.

Debounce the page provider

If your pageProvider calls a network API, set pageProviderDebounceTime to about 150 milliseconds.

<template>
  <div class="h-96 overflow-y-auto">
    <VirtualGrid
      :length="length"
      :page-size="40"
      :page-provider="pageProvider"
      :page-provider-debounce-time="150"
    >
      <template #default="{ item, index, style }">
        <div :style="style" :data-index="index">{{ item.name }}</div>
      </template>
    </VirtualGrid>
  </div>
</template>

During a fast scroll, the rendered window crosses dozens of pages in a second. Without a debounce, the grid requests every one of them, and the server prepares pages the user never sees. With a debounce, the grid waits until the scroll has paused for 150 ms, then requests only the pages in the rendered window it stopped on. About 150 ms is short enough that a deliberate scroll still loads quickly, and long enough to skip the pages a fast flick crosses.

With the default of 0, the grid requests pages at once. That is fine for createPageProvider, which reads an array in memory.

Reuse query objects in viewport-scroll handlers

The grid emits viewport-scroll on every scroll event. Create the query object once, outside the handler, and update its fields on each call.

<script setup lang="ts">
import { useTemplateRef } from "vue";
import { createRectQuery, getItemRect } from "vvirtual-grid/primitives";

const grid = useTemplateRef("grid");
const query = createRectQuery();

function onViewportScroll() {
  query.root = grid.value.$el;
  query.layout = grid.value.measurement;
  query.index = 42;
  if (getItemRect(query)) {
    // query.rect now holds the item's position on screen
  }
}
</script>

Bind the handler to the viewport-scroll event.

<template>
  <div class="h-96 overflow-y-auto">
    <VirtualGrid
      ref="grid"
      :length="length"
      :page-size="40"
      :page-provider="pageProvider"
      @viewport-scroll="onViewportScroll"
    >
      <template #default="{ item, index, style }">
        <div :style="style" :data-index="index">{{ item.name }}</div>
      </template>
    </VirtualGrid>
  </div>
</template>

getItemRect writes its result into query.rect and creates no object of its own. This is the same idea as an output parameter in a C API: get_rect(index, &rect) fills a struct the caller owns, instead of returning a new one.

This matters because every new object becomes garbage. The browser has to collect it, and it does so while the user scrolls. A handler that creates one object per scroll event creates thousands during a long scroll.

Read layout before writing styles

When you move an element in a viewport-scroll handler, read every position first, then write every style.

import { useTemplateRef } from "vue";

const popover = useTemplateRef("popover");

function onViewportScroll() {
  query.root = grid.value.$el;
  query.layout = grid.value.measurement;
  query.index = 42;
  if (!getItemRect(query)) return;
  const top = query.rect.top;

  popover.value.style.transform = `translateY(${top}px)`;
}
<template>
  <div class="h-96 overflow-y-auto">
    <VirtualGrid
      ref="grid"
      :length="length"
      :page-size="40"
      :page-provider="pageProvider"
      @viewport-scroll="onViewportScroll"
    >
      <template #default="{ item, index, style }">
        <div :style="style" :data-index="index">{{ item.name }}</div>
      </template>
    </VirtualGrid>
  </div>
  <div ref="popover" style="position: fixed" />
</template>

The handler uses the query from Reuse query objects in viewport-scroll handlers.

With root set, getItemRect reads the layout of one element. If you change a style and then read a position, the browser has to recompute the layout of the page on the spot to give you a correct answer. Doing that in a loop, once per element, is called layout thrashing. Reading all positions first lets the browser compute the layout once.

Key items by id with getKey

By default, the grid gives each rendered slot element a key made from its index. Vue uses the key to match an element to its data when the list updates. With index keys, the element at index 5 is always matched to the data at index 5.

When your list changes, for example when an item is inserted at the top, every index shifts by one. Vue then updates the content of every element instead of moving them.

Pass getKey to key the slot elements by your own id. Vue then matches each element to the item with that id, and moves the element when the item moves.

<template>
  <div class="h-96 overflow-y-auto">
    <VirtualGrid
      :length="length"
      :page-size="40"
      :page-provider="pageProvider"
      :get-key="(internalItem) => internalItem.value?.id ?? internalItem.index"
    >
      <!-- the default and placeholder slots -->
    </VirtualGrid>
  </div>
</template>

A placeholder has no value yet, so the function falls back to the index.

Keep slot content small

The grid renders the default and placeholder slots for every item in the window, and again each time the window moves by a row. A nested component tree costs once for each of the few hundred items in the window. Keep the markup in each slot to what the item shows, and move anything heavy, such as an image gallery or a menu, to the moment the user opens the item.

Check the result

Open the browser's developer tools and scroll fast through the whole list.

  1. In the Network panel, filter the requests by the API URL, then scroll to the end. With a network provider and the debounce set, the count stays small, not one request per page crossed.
  2. In the Performance panel, click Record, scroll the grid, then stop. Look for purple "Layout" blocks in the recording. A forced layout has a red triangle in its corner. Handlers that read before they write still show Layout blocks, because the browser lays out the page after the styles change. They do not show the red forced-layout marker.
Copyright © 2026