Performance: v-show vs v-if difference
This concept affects how quickly elements appear or disappear on the page and how much work the browser does to update the DOM.
<template> <div v-show="isVisible">Content</div> </template> <script setup> import { ref } from 'vue' const isVisible = ref(true) </script>
<template> <div v-if="isVisible">Content</div> </template> <script setup> import { ref } from 'vue' const isVisible = ref(true) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| v-if toggle | Adds/removes nodes | 1 reflow per toggle | 1 paint per toggle | [X] Bad for frequent toggling |
| v-show toggle | No DOM changes | 0 reflows | 1 paint per toggle | [OK] Good for frequent toggling |
| v-if initial render hidden | No DOM nodes rendered | 0 reflows | 0 paint | [OK] Good for rare rendering |
| v-show initial render hidden | Nodes always in DOM | 0 reflows | 0 paint | [!] OK but increases DOM size |
v-if and v-show in Vue?v-if behaviorv-if conditionally adds or removes elements from the DOM based on the condition.v-show behaviorv-show keeps the element in the DOM but toggles its visibility using CSS display property.v-if adds/removes elements from the DOM; v-show toggles visibility with CSS. -> Option Av-if = DOM add/remove, v-show = CSS toggle [OK]v-if changes DOM, v-show changes CSS [OK]v-show removes elements from DOMv-show in a Vue template?v-directive='expression' without colons or parentheses.<div v-show='isVisible'>Content</div> uses correct syntax: v-show='isVisible'. Others misuse colons or parentheses.v-show='condition' [OK]<template>
<div>
<p v-if='show'>Hello</p>
<p v-show='show'>World</p>
</div>
</template>show is initially false, what will be rendered in the DOM?v-if='show' with show = falsev-if removes the element from the DOM if the condition is false, so <p>Hello</p> is not in the DOM.v-show='show' with show = falsev-show keeps the element in the DOM but hides it with CSS, so <p>World</p> is present but not visible.v-if=false removes, v-show=false hides [OK]v-if=false removes element; v-show=false hides it [OK]v-show removes element<div v-if='isVisible'>Visible</div> <div v-show='isVisible'>Also Visible</div>
isVisible is undefined, what is the problem and how to fix it?v-if and v-showv-if treats undefined as false, so the element won't render. v-show treats undefined as false and keeps the element in the DOM but hides it using CSS display: none.isVisible properlyisVisible in data or state as a boolean (true or false) to avoid unexpected behavior.v-if element won't render; v-show element is in the DOM but hidden; fix by defining isVisible as a boolean. -> Option Dv-if and v-show [OK]v-if and v-showv-if adds/removes elements, so it delays rendering until shown, ideal for heavy content toggled infrequently.v-if because it avoids rendering heavy content until needed. -> Option Cv-if [OK]v-if to delay rendering [OK]v-show which renders heavy content alwaysPass the test and this pattern moves unknown