Performance: v-memo for conditional memoization
Improves rendering speed by skipping updates to parts of the DOM when their dependencies haven't changed.
<template> <div v-memo="[someReactive.value]"> <p>{{ expensiveComputation }}</p> </div> </template> <script setup> import { computed, ref } from 'vue' const someReactive = ref(0) const expensiveComputation = computed(() => { // heavy calculation return someReactive.value * 1000 }) </script>
<template>
<div>
<p>{{ expensiveComputation }}</p>
</div>
</template>
<script setup>
import { computed, ref } from 'vue'
const someReactive = ref(0)
const expensiveComputation = computed(() => {
// heavy calculation
return someReactive.value * 1000
})
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without v-memo | Updates every reactive change | Triggers reflow on each update | Higher paint cost due to frequent updates | [X] Bad |
| With v-memo | Updates only when dependencies change | Single reflow per dependency change | Lower paint cost by skipping unnecessary updates | [OK] Good |
v-memo in Vue?v-memo doesv-memo watches specific values and skips re-rendering if they remain unchanged.v-memo's purpose.v-memo watching variables count and user.name?v-memov-memo expects an array of values to watch, so the correct syntax uses square brackets.<div v-memo="[count, user.name]">... uses an array with square brackets, which is correct. Options A, B, and C use incorrect formats (logical AND, comma-separated without brackets, object).<template>
<div v-memo="[count]">Count is {{ count }}</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
setTimeout(() => { count.value = 0 }, 1000)
</script>v-memo watches count. After 1 second, count.value is set to 0, which is the same as its initial value.v-memo skips re-rendering the div.v-memo:<template>
<div v-memo="count">Count: {{ count }}</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>v-memo syntax requirementsv-memo requires an array of values to watch, so the value must be inside square brackets.v-memo="count" without brackets, which is incorrect syntax.status and value. You only want to re-render an item if its status changes, ignoring value. How should you use v-memo inside the v-for loop?item.status changes, ignoring item.value.v-memo with the correct watched valuev-memo="[item.status]" watches only the status, so changes in value won't trigger re-render.Pass the test and this pattern moves unknown