Performance: Keep-alive for expensive components
MEDIUM IMPACT
This affects the rendering speed and responsiveness by caching component state and DOM to avoid re-creation on each render.
<template>
<keep-alive>
<ExpensiveComponent v-if="show" />
</keep-alive>
</template>
<script setup>
import ExpensiveComponent from './ExpensiveComponent.vue'
import { ref } from 'vue'
const show = ref(true)
</script><template> <ExpensiveComponent v-if="show" /> </template> <script setup> import ExpensiveComponent from './ExpensiveComponent.vue' import { ref } from 'vue' const show = ref(true) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without keep-alive | Creates and destroys DOM nodes each toggle | Triggers full reflow on each toggle | High paint cost due to DOM rebuild | [X] Bad |
| With keep-alive | Reuses cached DOM nodes, minimal DOM ops | No reflow from component recreation | Low paint cost, only visibility changes | [OK] Good |