Performance: Why component patterns matter
This affects how fast the page loads and how smoothly the app responds to user actions by controlling rendering and updates.
<script setup> const items = ref(Array(1000).fill(0)) </script> <template> <div> <ItemComponent v-for="(item, index) in items" :key="index" :item="item" /> </div> </template> <script> export default { props: ['item'], setup() { // use shallow reactive or memoized computed to avoid unnecessary updates } } </script>
<script setup> const items = ref(Array(1000).fill(0)) </script> <template> <div> <ItemComponent v-for="(item, index) in items" :key="index" :item="item" /> </div> </template> <script> export default { props: ['item'], setup() { // heavy computed or watchers inside each item } } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy reactive dependencies in many child components | High (many nodes updated) | High (many reflows) | High (many paints) | [X] Bad |
| Memoized props and shallow reactive data | Low (only changed nodes) | Low (few reflows) | Low (minimal paints) | [OK] Good |
export default { setup() { return { count: 0 } } } correctly defines setup() returning an object. export default function setup() { return { count: 0 } } is invalid syntax. export default { data() { return { count: 0 } } } uses Options API, not Composition API. export default { setup: () => { count: 0 } } has incorrect arrow function syntax.export default { setup() { return { count: 0 } } } [OK]export default {
props: { count: Number },
emits: ['increment'],
setup(props, { emit }) {
function add() { emit('increment') }
return { add }
}
}add function is called?import { ref } from 'vue';
export default {
setup() {
const count = ref(0)
function increment() {
count++
}
return { count, increment }
}
}Pass the test and this pattern moves unknown