Performance: v-show vs v-if difference
MEDIUM IMPACT
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 |