Performance: Keep-alive for caching components
MEDIUM IMPACT
This affects page interaction speed by caching component state and reducing re-renders during navigation.
<template>
<keep-alive>
<component :is="currentView" />
</keep-alive>
</template>
<script setup>
import { ref } from 'vue'
const currentView = ref('HomeView')
</script><template> <component :is="currentView" /> </template> <script setup> import { ref } from 'vue' const currentView = ref('HomeView') </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without keep-alive | Destroys and recreates DOM nodes on each switch | Triggers 1 reflow per component switch | High paint cost due to full redraw | [X] Bad |
| With keep-alive | Reuses cached DOM nodes, no destruction | No reflow on cached component switch | Low paint cost, only compositing | [OK] Good |