Performance: Why event handling matters
HIGH IMPACT
Event handling affects how quickly the page responds to user actions and how much work the browser must do during interactions.
<template> <ul @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 li = event.target.closest('li'); if (!li) return; const id = Number(li.dataset.id); const item = items.find(i => i.id === id); if (item) console.log(item.name); } </script>
<template>
<ul>
<li v-for="item in items" :key="item.id" @click="handleClick(item)">{{ item.name }}</li>
</ul>
</template>
<script setup>
const items = Array(1000).fill(0).map((_, i) => ({ id: i, name: `Item ${i}` }));
function handleClick(item) {
console.log(item.name);
}
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Individual listeners on many elements | Many listeners attached | 0 | 0 | [X] Bad |
| Single delegated listener on parent | One listener attached | 0 | 0 | [OK] Good |