Performance: Why conditional rendering matters
MEDIUM IMPACT
Conditional rendering affects how many elements the browser must create and manage, impacting page load and interaction speed.
<template> <div v-if="isVisible">Content</div> </template> <script setup> import { ref } from 'vue' const isVisible = ref(false) </script>
<template> <div v-show="isVisible">Content</div> </template> <script setup> import { ref } from 'vue' const isVisible = ref(false) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| v-show toggling visibility | Element always in DOM | Triggers reflow on toggle | Paint triggered on toggle | [X] Bad |
| v-if conditional rendering | Element added/removed as needed | Reflow only when element changes | Paint only when element changes | [OK] Good |