Performance: v-model with select dropdowns
MEDIUM IMPACT
This affects how quickly the dropdown updates the UI and how many DOM updates happen when the user selects an option.
<template> <select v-model="selectedId"> <option v-for="item in items" :key="item.id" :value="item.id"> {{ item.name }} </option> </select> </template> <script setup> import { ref, computed } from 'vue' const selectedId = ref(null) const items = ref(Array(1000).fill(0).map((_, i) => ({ id: i, name: `Item ${i}` }))) const selected = computed(() => items.value.find(item => item.id === selectedId.value)) </script>
<template> <select v-model="selected"> <option v-for="item in items" :key="item.id" :value="item"> {{ item.name }} </option> </select> </template> <script setup> import { ref } from 'vue' const selected = ref(null) const items = ref(Array(1000).fill(0).map((_, i) => ({ id: i, name: `Item ${i}` }))) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Binding full objects as option values | High (many reactive dependencies) | Multiple reflows on selection | High paint cost due to re-renders | [X] Bad |
| Binding primitive IDs as option values | Low (simple reactive refs) | Single reflow on selection | Low paint cost | [OK] Good |