Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Enforce Vue 3 best practices—Composition API, script setup, TypeScript, component boundaries, and reactivity patterns
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/perf-virtualize-large-lists.md
1---2title: Virtualize Large Lists to Avoid DOM Overload3impact: HIGH4impactDescription: Rendering thousands of list items creates excessive DOM nodes, causing slow renders and high memory usage5type: efficiency6tags: [vue3, performance, virtual-list, large-data, dom, optimization]7---89# Virtualize Large Lists to Avoid DOM Overload1011**Impact: HIGH** - Rendering all items in a large list (hundreds or thousands) creates massive amounts of DOM nodes. Each node consumes memory, slows down initial render, and makes updates expensive. List virtualization only renders visible items, dramatically improving performance.1213Use a virtualization library when dealing with lists that could exceed 50-100 items, especially if items have complex content.1415## Task List1617- Identify lists that render more than 50-100 items18- Install a virtualization library (vue-virtual-scroller, @tanstack/vue-virtual)19- Replace standard `v-for` with virtualized component20- Ensure list items have consistent or estimable heights21- Test with realistic data volumes during development2223## Recommended Libraries2425| Library | Best For | Notes |26|---------|----------|-------|27| `vue-virtual-scroller` | General use, easy setup | Most popular, good defaults |28| `@tanstack/vue-virtual` | Complex layouts, headless | Framework-agnostic, flexible |29| `vue-virtual-scroll-grid` | Grid layouts | 2D virtualization |30| `vueuc/VVirtualList` | Naive UI projects | Part of Naive UI ecosystem |3132**BAD:**33```vue34<template>35<!-- BAD: Renders ALL 10,000 items immediately -->36<div class="user-list">37<UserCard38v-for="user in users"39:key="user.id"40:user="user"41/>42</div>43</template>4445<script setup>46import { ref, onMounted } from 'vue'47import UserCard from './UserCard.vue'4849const users = ref([])5051onMounted(async () => {52// 10,000 DOM nodes created, browser struggles53users.value = await fetchAllUsers()54})55</script>56```5758**GOOD:**59```vue60<template>61<!-- GOOD: Only renders ~20 visible items at a time -->62<RecycleScroller63class="user-list"64:items="users"65:item-size="80"66key-field="id"67v-slot="{ item }"68>69<UserCard :user="item" />70</RecycleScroller>71</template>7273<script setup>74import { ref, onMounted } from 'vue'75import { RecycleScroller } from 'vue-virtual-scroller'76import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'77import UserCard from './UserCard.vue'7879const users = ref([])8081onMounted(async () => {82// 10,000 items in memory, but only ~20 DOM nodes83users.value = await fetchAllUsers()84})85</script>8687<style scoped>88.user-list {89height: 600px; /* Container must have fixed height */90}91</style>92```9394## Using @tanstack/vue-virtual9596```vue97<template>98<div ref="parentRef" class="list-container">99<div100:style="{101height: `${rowVirtualizer.getTotalSize()}px`,102position: 'relative'103}"104>105<div106v-for="virtualRow in rowVirtualizer.getVirtualItems()"107:key="virtualRow.key"108:style="{109position: 'absolute',110top: 0,111left: 0,112width: '100%',113height: `${virtualRow.size}px`,114transform: `translateY(${virtualRow.start}px)`115}"116>117<UserCard :user="users[virtualRow.index]" />118</div>119</div>120</div>121</template>122123<script setup>124import { ref } from 'vue'125import { useVirtualizer } from '@tanstack/vue-virtual'126127const users = ref([/* 10,000 users */])128const parentRef = ref(null)129130const rowVirtualizer = useVirtualizer({131count: users.value.length,132getScrollElement: () => parentRef.value,133estimateSize: () => 80, // Estimated row height134overscan: 5 // Render 5 extra items above/below viewport135})136</script>137138<style scoped>139.list-container {140height: 600px;141overflow: auto;142}143</style>144```145146## Dynamic Heights with vue-virtual-scroller147148```vue149<template>150<!-- For variable height items, use DynamicScroller -->151<DynamicScroller152:items="messages"153:min-item-size="54"154key-field="id"155>156<template #default="{ item, index, active }">157<DynamicScrollerItem158:item="item"159:active="active"160:data-index="index"161>162<ChatMessage :message="item" />163</DynamicScrollerItem>164</template>165</DynamicScroller>166</template>167168<script setup>169import { DynamicScroller, DynamicScrollerItem } from 'vue-virtual-scroller'170</script>171```172173## Performance Comparison174175| Approach | 100 Items | 1,000 Items | 10,000 Items |176|----------|-----------|-------------|--------------|177| Regular v-for | ~100 DOM nodes | ~1,000 DOM nodes | ~10,000 DOM nodes |178| Virtualized | ~20 DOM nodes | ~20 DOM nodes | ~20 DOM nodes |179| Initial render | Fast | Slow | Very slow / crashes |180| Virtualized render | Fast | Fast | Fast |181182## When NOT to Virtualize183184- Lists under 50 items with simple content185- Lists where all items must be accessible to screen readers simultaneously186- Print layouts where all content must render187- SEO-critical content that must be in initial HTML188