Performance: Why Vue performance matters
Vue performance affects how fast the page loads and how smoothly users can interact with the app.
<template>
<ul>
<li v-for="item in items" :key="item.id"> {{ item.name }} </li>
</ul>
</template><template>
<ul>
<li v-for="item in items" :key="Math.random()"> {{ item.name }} </li>
</ul>
</template>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Random keys in v-for | Recreates all nodes each update | N reflows per update | High paint cost | [X] Bad |
| Stable unique keys in v-for | Reuses nodes efficiently | Single reflow per update | Low paint cost | [OK] Good |
<template>
<div>
<p>Count: {{ count }}</p>
<button @click="increment">Add</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
count.value++
}
</script><template>
<div>
<p>Message: {{ message }}</p>
<button @click="updateMessage">Change</button>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({ message: 'Hello' })
function updateMessage() {
state.message = 'Hi'
state = { message: 'Hey' }
}
</script>v-for with a unique :key and virtual scrolling. -> Option APass the test and this pattern moves unknown