
The problem
When you render a long list with v-for, Vue re-renders every row whenever any reactive value in the component changes — even rows whose own data never moved. In a 1,000-row table driven by a search box, a hover state, or a selected-id ref, that is a lot of wasted work on every keystroke or click.
Enter v-memo
v-memo tells Vue: “only re-render this element (and its subtree) when these specific dependencies change.” You pass it an array of the values that actually matter to the row.
<template>
<ul>
<li
v-for="item in items"
:key="item.id"
v-memo="[item.id === selectedId, item.price]"
>
{{ item.name }} — ${{ item.price }}
<span v-if="item.id === selectedId">selected</span>
</li>
</ul>
</template>
Here each row re-renders only when its own price changes or its selected state flips. Scrolling, or toggling another row’s selection, will not re-render this row.
A complete, runnable example
<script setup>
import { ref } from 'vue'
const items = ref(
Array.from({ length: 5000 }, (_, i) => ({
id: i,
name: `Item #${i}`,
price: Math.floor(Math.random() * 100),
}))
)
const selectedId = ref(null)
function select(id) { selectedId.value = id }
</script>
<template>
<div>
<button @click="select(Math.floor(Math.random() * 5000))">
Select random
</button>
<ul>
<li
v-for="item in items"
:key="item.id"
v-memo="[item.id === selectedId]"
@click="select(item.id)"
>
{{ item.name }} — ${{ item.price }}
<strong v-if="item.id === selectedId">(selected)</strong>
</li>
</ul>
</div>
</template>
Clicking around now only re-renders the two rows whose highlight actually changed — not all 5,000 of them.
Gotchas
v-memois per-element, not per-loop. Put it on thev-forelement itself (as above) to memoize each individual row.- The memo array must capture every value that should trigger a refresh. Miss one and the row silently shows stale content — the hardest bug to spot with this directive.
- Do not memoize tiny or simple lists. The bookkeeping can cost more than the render it saves. Reach for it when a list is large, or rows are expensive (heavy child components, in-row computed work).
v-memo="[]"with an empty array means “never re-render after first paint.” Use it only for truly static content, because it really will freeze the node.
When to reach for it
- Long
v-forlists (hundreds of rows and up). - Rows containing expensive child components.
- A frequently-changing reactive value (a timer, a filter string, a hover index) that does not touch most rows.
For component-level reuse, keep row components small and consider defineAsyncComponent for off-screen, heavy rows. v-memo is the cheap, surgical win that keeps the rest of your reactivity model untouched.