Performance: Why advanced patterns matter
HIGH IMPACT
This concept affects how efficiently Vue apps render and update, impacting page load speed and interaction responsiveness.
<template>
<ul>
<ListItem v-for="item in items" :key="item.id" :item="item" />
</ul>
</template>
<script setup>
import { ref, reactive } from 'vue'
import ListItem from './ListItem.vue'
const items = ref([...Array(1000)].map((_, i) => reactive({ id: i, name: `Item ${i}` })))
function updateAll() {
items.value.forEach(item => { item.name += ' updated' })
}
</script><template>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([...Array(1000)].map((_, i) => ({ id: i, name: `Item ${i}` })))
function updateAll() {
items.value = items.value.map(item => ({ ...item, name: item.name + ' updated' }))
}
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Recreate entire list on update | Many DOM nodes replaced | 1000 reflows | High paint cost | [X] Bad |
| Update items in place with child components | Minimal DOM changes | Few reflows | Low paint cost | [OK] Good |