Introduction
A page that renders 1,000,000 items as DOM elements creates 1,000,000 elements, and the browser lays out every one before the page is usable. vvirtual-grid is a Vue 3 component that avoids this. It lays the list out as a CSS grid and keeps only the items near the viewport in the page.
A list of 1,000,000 items keeps about the same few hundred elements as a list of 1,000.
1,000,000 items, 0 elements in the DOM.
Scroll the grid. The element count in the demo stays flat while the item numbers climb toward 999,999.
vvirtual-grid requires Vue 3.5 or later. Nuxt 3 and 4 work through the included module.What the package contains
<VirtualGrid>is the component. You give it the number of items and a function that returns a page of them.createPageProviderwraps an array in memory in that function, so you can start without a server.- The primitives in
vvirtual-grid/primitivescompute where an item is, even when it is not in the page. You use them for accessibility, focus and popovers.
When to use TanStack Virtual instead
vvirtual-grid does four things that a general-purpose virtualizer leaves to you. If your project needs none of them, use TanStack Virtual.
- It loads the data in pages. You pass a
pageProviderfunction. The grid calls it for each page that scrolls into view, remembers the result, and renders a placeholder until the page arrives. With TanStack Virtual, you write the loading, the cache and the placeholders. - It reads the layout from your CSS. The grid takes its columns, gaps and flow from the
grid-template-columns,gapandgrid-auto-flowyou set on it. Arepeat(auto-fill, minmax(200px, 1fr))grid changes its column count with the viewport, with no code. With TanStack Virtual, you compute the column count in JavaScript, and again after every resize. - It renders each item as a direct child of one grid element. There are no row wrappers, so
role="list"on the grid androle="listitem"on the items describe the markup as it is. See Accessibility. - It renders the first page on the server. The HTML holds the first items before any script has measured the page.
TanStack Virtual is the better choice in three cases.
- Your items differ in size. Every item in
vvirtual-gridhas the size of the probe. TanStack Virtual measures each item. - You render a list, a table or a masonry layout.
vvirtual-gridrenders grids only. - Bundle size and the first render matter most. In our test of 1,000,000 items in 10 columns, TanStack Virtual adds 9.8 KB of gzipped JavaScript and shows its first items 23.5 ms after mount.
vvirtual-gridadds 13.8 KB and takes 63.2 ms, because it measures the probe and requests the first page before it places items.
How the grid decides what to render
The grid measures one hidden element, called the probe, to learn the size of an item. It combines that size with how far you have scrolled to work out which items are on screen. It renders those items plus a buffer of extra items above and below, asks your function for the pages it needs, and places each item with a CSS transform.
The Architecture page follows this step by step, from the props to the DOM.
Next steps
- Installation adds the package to a Vue or Nuxt project and renders a first grid.
- Architecture explains the internals for contributors and advanced users.
- Accessibility makes a screen reader announce item positions.
- Popovers anchors an element to an item.