0
0
Vueframework~8 mins

v-show vs v-if difference in Vue - Performance Comparison

Choose your learning style9 modes available
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.
Toggling visibility of an element frequently
Vue
<template>
  <div v-show="isVisible">Content</div>
</template>
<script setup>
import { ref } from 'vue'
const isVisible = ref(true)
</script>
v-show only toggles CSS display property, avoiding DOM changes and layout recalculations.
📈 Performance GainSingle repaint per toggle, no reflow triggered
Toggling visibility of an element frequently
Vue
<template>
  <div v-if="isVisible">Content</div>
</template>
<script setup>
import { ref } from 'vue'
const isVisible = ref(true)
</script>
v-if adds or removes the element from the DOM each time, causing layout recalculations and reflows.
📉 Performance CostTriggers 1 reflow and 1 repaint every toggle
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
v-if toggleAdds/removes nodes1 reflow per toggle1 paint per toggle[X] Bad for frequent toggling
v-show toggleNo DOM changes0 reflows1 paint per toggle[OK] Good for frequent toggling
v-if initial render hiddenNo DOM nodes rendered0 reflows0 paint[OK] Good for rare rendering
v-show initial render hiddenNodes always in DOM0 reflows0 paint[!] OK but increases DOM size
Rendering Pipeline
v-if adds or removes elements from the DOM, triggering style recalculation, layout, and paint. v-show toggles CSS display, triggering only paint without layout recalculation.
Style Calculation
Layout
Paint
⚠️ BottleneckLayout stage is most expensive for v-if due to DOM changes
Core Web Vital Affected
INP
This concept affects how quickly elements appear or disappear on the page and how much work the browser does to update the DOM.
Optimization Tips
1Use v-if to avoid rendering elements until needed, reducing initial load.
2Use v-show to toggle visibility quickly without DOM changes or layout recalculations.
3Avoid v-show for large hidden content to keep DOM size small and improve load speed.
Performance Quiz - 3 Questions
Test your performance knowledge
Which directive causes the browser to add or remove elements from the DOM?
Av-bind
Bv-show
Cv-if
Dv-on
DevTools: Performance
How to check: Record a performance profile while toggling visibility; observe layout and paint events in the flame chart.
What to look for: Frequent layout events indicate v-if toggling; paint-only events indicate v-show toggling.