Performance: Key attribute and why it matters
HIGH IMPACT
This affects how Vue efficiently updates the DOM during re-rendering, impacting interaction responsiveness and visual stability.
<template>
<ul>
<li v-for="item in items" :key="item.id"> {{ item.name }} </li>
</ul>
</template><template>
<ul>
<li v-for="(item, index) in items" :key="index"> {{ item.name }} </li>
</ul>
</template>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using index as key | Many node removals and insertions | Multiple reflows per update | High paint cost due to layout shifts | [X] Bad |
| Using unique id as key | Minimal node updates | Single reflow per update | Low paint cost, stable layout | [OK] Good |