Performance optimization in Vue
Where Vue apps get slow and the fix for each: stable props, v-once, v-memo, and shallowRef for rendering, virtual lists, and lazy loading for bundles.
Shallow reactivity and escape hatchesKeepAlive and cached componentsAsync components and Suspense
Performance optimization in Vue
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Vue's fine-grained reactivity and compiler already skip most unnecessary work, so measure first; most real Vue performance problems are either too much JavaScript on page load or too many DOM nodes, not slow re-renders.
- A child component re-renders only when the props it receives change, so passing each list item a derived boolean like active instead of the shared activeId keeps a selection change from updating every row.
- v-once renders a subtree a single time and never updates it; v-memo skips updating a subtree until one of the values in its dependency array changes, and is meant for large v-for lists after measurement shows a need.
- shallowRef and markRaw stop Vue from making large data or third-party objects deeply reactive, which saves the cost of wrapping every nested property in a proxy.
- A virtual list renders only the rows inside the viewport, so a list of 50,000 items costs about as much DOM as a list of 30.
- Lazy-loading routes and heavy components with dynamic import() splits the bundle so each page downloads only the code it needs.
Measure before you optimise
Vue starts from a good place. Its reactivity system knows exactly which components read which state, so when something changes only those components re-render, with no need for the memo/useMemo/useCallback habits React developers carry around. Its template compiler also marks static parts of a template so the runtime skips them during updates.
So the first rule is to find out whether there is a problem and where it is. In practice, Vue apps that feel slow almost always have one of three problems:
- Too much JavaScript on first load. The user waits for a large bundle to download and parse before anything works.
- Too many DOM nodes. A list or table renders thousands of rows, and no framework makes creating 50,000 elements cheap.
- Updates that touch more components than they need to. Rarer, but real in large lists and dashboards.
Use the browser’s Performance panel and Vue DevTools to tell which one you have. Page-load metrics themselves (LCP, INP, CLS) are covered in web performance and Core Web Vitals.
Keep props stable
A child component re-renders when one of its props changes. That sounds obvious until you see how easily a list passes every row a value that changes for all of them:
<!-- Every row re-renders when selectedId changes -->
<ListItem
v-for="item in items"
:key="item.id"
:item="item"
:selected-id="selectedId"
/>
Select a different row and selectedId changes, so every ListItem gets a new prop value and re-renders, even though only two rows look different. Move the comparison up into the parent:
<!-- Only the two rows whose `selected` flips re-render -->
<ListItem
v-for="item in items"
:key="item.id"
:item="item"
:selected="item.id === selectedId"
/>
Now 998 rows receive false before and after the change, so Vue skips them. This “prop stability” habit is the single most useful rendering optimisation in Vue, and it costs nothing.
Computed values help in the same way. Since Vue 3.4, a computed only triggers its dependents when its value actually changes, so a computed returning a boolean or a number can absorb many upstream changes without causing re-renders downstream.
v-once and v-memo
Two directives let you tell the compiler more than it can work out on its own.
v-once renders an element and its children a single time and then treats them as static, ignoring later changes to the data they read:
<footer v-once>Terms accepted on {{ formatDate(acceptedAt) }}</footer>
Use it for content that is computed from data but will never need to change while the component lives.
v-memo is a conditional version. It takes an array of values and skips updating the subtree, including diffing its virtual DOM, until one of them changes:
<div
v-for="item in items"
:key="item.id"
v-memo="[item.id === selectedId, item.label]"
>
<p>{{ item.label }}</p>
<p :class="{ active: item.id === selectedId }">{{ item.details }}</p>
</div>
Two rules. When used with v-for, put v-memo on the same element as v-for; it doesn’t work on a child inside it. And list every value the subtree depends on, or it will keep showing old data, since you’ve told Vue not to look. The Vue docs describe v-memo as a micro-optimisation for performance-critical cases, such as very large v-for lists, not something to sprinkle everywhere. With stable props it’s rarely needed.
Shallow reactivity for big data
By default, ref({...}) and reactive({...}) make data deeply reactive. Vue wraps nested objects in proxies as you access them and tracks every property read. For app state that you edit field by field, that is exactly what you want. For a 20,000-row API response you only ever replace in full, it is wasted work.
import { shallowRef, markRaw } from 'vue'
const rows = shallowRef<Row[]>([])
rows.value = await fetchRows() // triggers an update
rows.value[0].name = 'x' // does NOT trigger an update
rows.value = [...rows.value] // replace to trigger
const map = markRaw(new maplibregl.Map({ container: 'map' })) // never proxied
shallowRef tracks only assignments to .value. markRaw excludes an object from reactivity entirely, which matters for third-party class instances like a map or chart, where a proxy wrapper costs time and can break the library’s own identity checks. The mechanics are covered in shallow reactivity and escape hatches.
Virtual lists for long lists
No rendering trick helps if you put 50,000 rows into the DOM. The browser has to create, style, and lay out every one. A virtual list renders only the rows visible in the viewport, plus a small buffer, and swaps their contents as you scroll, keeping the scrollbar the right size with spacer elements.
Think of it as a window onto a long strip of paper: the paper can be miles long, but you only ever print the part behind the window.
<script setup lang="ts">
import { useVirtualList } from '@vueuse/core'
const props = defineProps<{ items: { id: number; name: string }[] }>()
const { list, containerProps, wrapperProps } = useVirtualList(
() => props.items,
{ itemHeight: 40 },
)
</script>
<template>
<div v-bind="containerProps" style="height: 480px">
<div v-bind="wrapperProps">
<div v-for="{ data, index } in list" :key="data.id" style="height: 40px">
{{ index }}: {{ data.name }}
</div>
</div>
</div>
</template>
VueUse’s useVirtualList suits fixed-height rows. For variable heights, grids, or tables, use @tanstack/vue-virtual or vue-virtual-scroller. The trade-offs: in-page search (Ctrl+F) can’t find rows that aren’t rendered, and accessibility needs care, so use a virtual list when the size of the list demands it, not by default.
Ship less JavaScript: lazy routes and components
For most apps, the biggest win is on first load. A dynamic import() tells the bundler to split a module into its own chunk that downloads on demand:
// router.ts
const routes = [
{ path: '/', component: Home }, // in the main bundle
{ path: '/reports', component: () => import('./views/Reports.vue') }, // own chunk
]
The same works for a heavy component that only appears sometimes, like a chart or a rich-text editor, using defineAsyncComponent:
const RichEditor = defineAsyncComponent(() => import('./RichEditor.vue'))
Two related habits keep the bundle lean. Avoid registering large libraries’ components globally, which forces them all into the main bundle (the first runner-up video demonstrates this with a bundle visualiser). And reach for <KeepAlive> when users switch back and forth between expensive views, so the component is cached instead of rebuilt each time.
React developers can map these directly: lazy routes are React.lazy, stable props are what React.memo relies on, and v-memo is closest to a memoised subtree. The difference is that in Vue you reach for them far less often.
What the video covers
In this DejaVue episode, Alexander Lichter and Michael Thiessen work through the same toolbox this lesson does: prop stability, v-once, v-memo, v-pre, avoiding unnecessary computed recalculation, virtualization, and shallowRef, then look ahead to Vapor Mode and SSR performance. It’s a podcast, so there’s little code on screen; the examples above supply it. Lazy-loading routes isn’t one of its chapters, which is why that section is here and why the Lichter bundle-size video is the first runner-up.
Where this fits
Performance builds on shallow reactivity, KeepAlive, and async components. Vapor Mode, the compilation strategy that removes the virtual DOM, gets its own lesson in Vapor Mode and the future of Vue. Next: debugging with Vue DevTools.
Check yourself
A list of 1,000 rows receives `:selected-id="selectedId"` on every row, and each row compares it to its own id. Clicking a row feels sluggish. Why, and what is the fix?
Every row receives `selectedId` as a prop, so when it changes all 1,000 rows see a changed prop and re-render, even though only two rows actually changed state. Passing a derived boolean instead, `:selected="item.id === selectedId"`, means only the row losing selection and the row gaining it get a new prop value, so only those two re-render.
What does `v-memo="[item.id === selectedId]"` on a `v-for` row do?
It caches the row's rendered output and skips re-rendering that row, including its vnode diff, unless the value inside the array changes. For most rows the comparison stays false before and after a selection change, so they are skipped entirely. It has to be on the same element as `v-for`, and any other value the row displays must also be in the array, or the row will show stale data.
Why would you store a 20,000-item API response in `shallowRef` instead of `ref`?
`ref` with an object value makes it deeply reactive, so Vue wraps every nested object in a proxy on access and tracks every property read. If the data is replaced wholesale rather than edited field by field, that tracking is pure overhead. `shallowRef` only tracks replacement of `.value`, so assigning a new array still triggers updates while nested reads cost nothing extra.
How does `component: () => import('./views/Reports.vue')` in a route definition change what the browser downloads?
The dynamic import tells the bundler to put `Reports.vue` and anything only it uses into a separate chunk. That chunk is downloaded the first time the user visits the route rather than as part of the initial bundle, which makes the first page load smaller and faster.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.