Vue3's v-if vs v-show: Rendering Internals, Branch Pitfalls, and the Cost of Each Toggle
Picking the wrong directive shows up as real page cost: v-if on a frequently toggled modal rebuilds the whole subtree on every open, while long-hidden v-show content keeps holding memory through live timers and listeners. The Vue2-to-Vue3 priority flip on v-if/v-for also means migrated code can develop a different, sneakier failure long after the upgrade.
Vue3's conditional rendering splits into two cost models. v-if and its v-else-if/v-else chain lazily create and destroy DOM: a false condition produces no VNode and no real node, and each toggle fires mounted/unmounted while rebuilding the tree. v-show keeps the DOM mounted forever and only toggles the CSS display property, so switching is nearly free but the initial render always happens and no lifecycle hooks run.
The decision rule comes down to toggle frequency: one-time or rare judgments such as permission gates, on-demand module loading, and initial page checks fit v-if; modals, drawers, tab switches, and repeatedly toggled buttons fit v-show. The failure modes are equally concrete: v-else must sit directly adjacent to its preceding branch or the chain silently breaks, multi-branch conditions must be ordered strict-to-loose or the first broad condition swallows later branches, and v-if plus v-for at the same level is off-limits — Vue2 looped first then filtered, while Vue3's reversed priority breaks the iteration. v-show cannot wrap a <template> tag, and hidden v-show content keeps timers, requests, listeners, and memory alive.
Vue 3 reversed the v-if/v-for priority compared with Vue 2, so the same prohibited mix now fails in a different direction after an upgrade: code that wasted cycles in Vue2 silently breaks the iteration in Vue3.
The selection rule reduces to a cost model: every v-if toggle pays full subtree teardown and rebuild, while every v-show initial mount pays for DOM that may never be seen, so the choice is about toggle frequency, not which directive is 'better'.
Vue compiles v-if/v-else-if/v-else as one contiguous branch structure, which is why a separator node breaks the pairing instead of being harmlessly ignored, and why branches must stay adjacent.