Performance: Generic components
MEDIUM IMPACT
Generic components affect rendering speed and bundle size by reusing code and reducing code duplication.
<template>
<div>
<Card v-for="item in items" :key="item.id" :title="item.title" :content="item.content" />
</div>
</template>
<script setup>
import Card from './Card.vue'
const items = [
{ id: 1, title: 'Title A', content: 'Content A' },
{ id: 2, title: 'Title B', content: 'Content B' },
{ id: 3, title: 'Title C', content: 'Content C' }
]
</script>
<!-- Card.vue -->
<template>
<div class="card">
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</template>
<script setup>
const props = defineProps(['title', 'content'])
</script><template>
<div>
<div class="card">
<h2>{{ title1 }}</h2>
<p>{{ content1 }}</p>
</div>
<div class="card">
<h2>{{ title2 }}</h2>
<p>{{ content2 }}</p>
</div>
<div class="card">
<h2>{{ title3 }}</h2>
<p>{{ content3 }}</p>
</div>
</div>
</template>
<script setup>
const title1 = 'Title A'
const content1 = 'Content A'
const title2 = 'Title B'
const content2 = 'Content B'
const title3 = 'Title C'
const content3 = 'Content C'
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated HTML blocks | High (due to code duplication) | Multiple reflows per block | High paint cost due to many elements | [X] Bad |
| Generic component with props | Low (single component reused) | Single reflow for list | Lower paint cost due to fewer nodes | [OK] Good |