Performance: v-on directive for events
MEDIUM IMPACT
This affects how quickly the page responds to user interactions and how efficiently event listeners are managed in the browser.
<template> <ul v-on:click="handleListClick"> <li v-for="item in items" :key="item.id" :data-id="item.id">{{ item.name }}</li> </ul> </template> <script setup> const items = Array(1000).fill(0).map((_, i) => ({ id: i, name: `Item ${i}` })); function handleListClick(event) { const id = event.target.dataset.id; if (id) console.log('Clicked', id); } </script>
<template>
<ul>
<li v-for="item in items" :key="item.id" v-on:click="handleClick(item.id)">{{ item.name }}</li>
</ul>
</template>
<script setup>
const items = Array(1000).fill(0).map((_, i) => ({ id: i, name: `Item ${i}` }));
function handleClick(id) {
console.log('Clicked', id);
}
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Individual v-on on many elements | Many listeners attached | 0 (no layout changes) | Low paint cost | [X] Bad |
| Single v-on on parent with delegation | One listener attached | 0 (no layout changes) | Low paint cost | [OK] Good |