Skip to content
Tutorials Center
My learning

Topics

Lesson 62 of 87 ยท Performance, Security and Ecosystem

Performance Basics

Learn how Vue decides what to re-render, why stable props and computed values matter, and when v-once, v-memo and shallow reactivity help, with render counters.

Vue is fast by default. Its compiler turns templates into code that skips static parts, and its reactivity system re-renders only the components whose data changed. For most pages you do not need to do anything special. Performance work is for the moments when something is measurably slow: a long list that stutters, a typing field that lags, a dashboard that burns CPU.

This lesson gives you the mental model and a short list of tools. You will learn to measure before changing anything, how Vue decides what to update, why stable props and computed values keep updates cheap, and when v-once, v-memo and shallow reactivity are worth using. The examples include render counters so you can watch the savings with your own eyes. The next lesson tackles big lists and lazy loading.

Measure first

The first rule of performance work: do not guess. Most code that looks slow is fine, and most real bottlenecks are somewhere you did not expect. Before optimizing, find out what is slow and by how much.

  • Browser Performance panel. Record while you do the slow thing (typing, scrolling, opening a menu). The flame chart shows where the time goes: scripting, rendering or painting.
  • Vue DevTools Timeline. Shows component updates and how long each took, which answers "which component keeps re-rendering?".
  • console.time / performance.now. Handy for timing one function.
  • Lighthouse and real-user data for page load (see Build and Load Performance).
  • app.config.performance = true in development adds Vue's component init, render and patch timings to the browser's Performance recording.

Always measure in a production build for final numbers, because the development build is deliberately slower (it does extra checks). A change that does not move the measurement is not an optimization, it is just more code.

How Vue decides what to update

When reactive state changes, Vue does not rebuild the page. It follows a short pipeline:

flowchart LR
    S["State changes<br/>(ref.value = ...)"] --> Q["Affected components<br/>queued once"]
    Q --> R["Component render function<br/>runs, returns new vnodes"]
    R --> D["Diff against<br/>previous vnodes"]
    D --> P["Patch only the changed<br/>DOM nodes"]

Updates are batched per component: ten changes in a row cause one render.

Three facts follow from this and explain most Vue performance advice:

  1. The unit of re-rendering is the component. If a component reads a ref in its template, a change to that ref re-runs that component's whole render function.
  2. A child component re-renders when its props change, or when its own state changes, not merely because its parent re-rendered. Vue compares each prop with the old value using strict equality.
  3. Strict equality means objects and arrays are compared by identity. A new object with identical contents counts as a change.

The compiler helps too. It marks static elements so they are created once and skipped on later updates, and it attaches "patch flags" to dynamic ones so the diff only checks what can change (a text node, a class). You get this for free from templates, which is one reason templates are usually faster than hand-written render functions.

Stable props keep children quiet

Let's make this visible. Four children sit inside one parent. The parent re-renders every time you click, because its template reads clicks. Each child shows how many times its own render function has run. (Counting renders by calling a function inside a template is a demo trick; never put side effects in real templates.)

  • Static gets a constant string.
  • Counter gets clicks itself, so it truly changes.
  • Fresh object gets a new object literal built from the parent's state on every render.
  • Stable object gets the same object every time.
Which children re-render?

Click the button three times. The parent has rendered four times in total, and so have the Counter and the Fresh object children. The Static child and the Stable object child are still at one render. The first two have a good reason, since their props really changed or look changed. The Stable object child is quiet even though it is reactive and the parent re-rendered, because its prop is the same object as before.

The lesson is not "avoid objects in props". Passing a fresh object usually costs a cheap re-render of a small child. It matters when the child is big or the parent re-renders very often (for example on every keystroke). In that case, pass primitives, pass a stable reference, or compute the object once with computed.

Computed values are stable

A computed property caches its result and, since Vue 3.4, it only notifies its dependents if the result actually changed. That means a computed can act as a filter between a frequently changing source and an expensive consumer.

The next example has a counter that changes on every click and a child that only cares whether the counter has passed 5. The parent's clicks changes constantly, but the computed isBig flips once.

A computed as a change filter

After ten clicks the badge has rendered twice: once on mount and once when clicks crossed 5. The parent itself rendered eleven times. The same trick works in your own components: derive the narrowest value a child needs and pass that.

Two related habits:

  • Put derived data in computed, not in a method called from the template, or the work repeats on every render. A computed runs again only when its inputs change.
  • Keep computed getters pure and cheap; do not mutate state or fetch inside them.

v-once and v-memo

Sometimes you know a piece of the template can be skipped, and you tell Vue so.

v-once renders an element and its children a single time. After that Vue treats it as static, even if the data it used changes.

HTML
<h1 v-once>{{ title }}</h1>
<footer v-once>Copyright {{ year }}</footer>

Use it for content that truly never changes after the first render. It is rarely the first tool you need, because Vue already skips static markup.

v-memo is more useful. It takes an array of dependencies and skips re-rendering a subtree unless one of the values in the array changed (compared with Object.is, like props). It shines inside v-for on long lists, where one row changes and hundreds should be left alone.

The two lists below are identical, except that the second uses v-memo="[item.id === selected]". Each row prints how many times it has rendered. Click rows in either list to move the selection, then compare the counts.

v-memo skips unchanged rows

Click row 3 in either list (the selection starts on row 1). Every row in the left list renders a second time, while on the right only rows 1 and 3 do: the row that lost the selection and the row that gained it. The other three keep their old output.

v-memo has a catch: you are responsible for listing every value the row depends on. If the row also showed item.name and you forgot to include it in the array, edits to the name would never appear. Treat v-memo as a measured fix for a long list, not a default.

An incomplete v-memo dependency array causes stale UI, a bug that is easy to miss. Add it only after a measurement shows the list is slow, and keep the array small and obvious.

Reactivity overhead

Reactivity is not free: when you make an object reactive, Vue wraps it in a proxy and tracks reads of its properties. For normal app state this cost is negligible. It adds up in a few situations:

  • Huge data sets. A ref with 100,000 objects is deeply reactive: every nested object becomes a proxy when first accessed. If you only ever replace the whole array (a table of query results, say), use shallowRef. Only .value is tracked, and nested objects stay plain. The next lesson shows this in detail.
  • Third-party objects. Chart instances, maps, editors and class instances with internal state should not be deeply reactive. Wrap them with markRaw() or store them in a shallowRef, so Vue does not proxy their internals (see How Reactivity Works).
  • Deep watchers. watch(source, cb, { deep: true }) walks every nested property on each change. Watch the specific field you care about, or a computed, instead.
  • Work in templates. Expressions in a template run on every render of that component. Move heavy work into computed.

Practical checklist

A short list of habits that give most of the benefit with little risk:

  1. Keys on every v-for with stable ids, never the index when items reorder (see List Rendering).
  2. Split large components. A small child that owns a fast-changing value re-renders alone, instead of dragging a big parent with it.
  3. Pass the narrowest props. Primitives and computed booleans beat whole objects.
  4. v-show for frequent toggles, v-if for rare ones. v-show keeps the DOM and flips CSS; v-if creates and destroys it (see Conditional Rendering).
  5. Debounce rapid input that triggers heavy work (search-as-you-type).
  6. Lazy-load routes and heavy components (see Dynamic and Async Components).
  7. Re-measure. Keep the change only if the numbers improved.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What should you do before optimizing a slow Vue page?

    Guessing wastes effort and adds complexity. Measure first, change one thing, and measure again.
  2. 2. A parent re-renders and passes a new object literal with the same contents as a prop. What does the child do?

    Vue uses strict equality for props. A new object is a different object, even if every field matches.
  3. 3. Why can a computed property reduce re-rendering of a child?

    A computed such as isBig = count > 5 changes rarely even if count changes constantly, so the child that receives isBig renders rarely.
  4. 4. What does v-memo="[item.id === selected]" on a list row do?

    v-memo compares the listed values with the previous render and reuses the old vnodes when none changed. You must list everything the row depends on.
  5. 5. A page loads 50,000 records into a ref and only ever replaces the array. Which change reduces reactivity overhead?

    A deep ref makes every nested record reactive on access. shallowRef tracks only replacement of the whole array, which is all this page needs.

Summary

  • Measure first, in a production build when possible; keep a change only if the numbers improve.
  • Vue updates per component: a child re-renders when its props change, and props are compared with strict equality.
  • Pass primitives, stable references or computed values to keep children quiet; a fresh object each render counts as a change.
  • A computed only notifies dependents when its result changes, which makes it a cheap change filter.
  • v-once renders once; v-memo skips subtrees (best on long v-for lists) but you must list every dependency.
  • Use shallowRef and markRaw for big or foreign data, avoid deep watchers, and move heavy template work into computed.

Next, apply these ideas to long lists and heavy components in Large Lists and Lazy Loading.