How does Vue compile templates, and what makes its rendering fast? What is Vapor mode?
From template to DOM
- The compiler parses the template into an AST and generates a render function (at build time with Vite, so no compiler ships to the browser).
- The render function returns a virtual DOM tree.
- On updates, Vue patches: compares the new tree with the old one and changes only the differences.
Compiler-informed virtual DOM
Unlike a plain virtual DOM, Vue's compiler knows which parts of a template can never change and passes hints to the runtime:
- Static hoisting: static elements are created once and reused, never diffed.
- Patch flags: each dynamic node is tagged with what can change (only its text, only
class, only certain props), so the patch skips everything else. - Block tree: dynamic nodes inside a block are collected into a flat array, so updates walk only those nodes instead of the whole tree. Structural directives (
v-if,v-for) start new blocks. - Cached event handlers: inline handlers are cached so children do not re-render.
Combined with fine-grained reactivity (only components whose dependencies changed re-render), this gives good performance with no manual memoisation.
Vapor mode
An alternative compilation strategy, developed for Vue 3.6, that compiles templates to direct DOM operations with no virtual DOM, similar to Solid and Svelte. It gives smaller bundles and faster updates, and can be enabled per component (<script setup vapor>) while the rest of the app uses the virtual DOM. Check its current release status before relying on it in production.
Example
<template>
<div class="card">
<h2>Static title</h2> <!-- hoisted: created once, never diffed -->
<p :class="tone">{{ message }}</p> <!-- patch flags: TEXT | CLASS only -->
<button @click="count++">{{ count }}</button>
</div>
</template>Roughly what the compiler generates:
const _hoisted = createElementVNode('h2', null, 'Static title', -1 /* HOISTED */);
function render(ctx) {
return (openBlock(), createElementBlock('div', { class: 'card' }, [
_hoisted,
createElementVNode('p', { class: normalizeClass(ctx.tone) }, toDisplayString(ctx.message), 3 /* TEXT, CLASS */),
createElementVNode('button', { onClick: ctx._cache[0] ||= ($e) => ctx.count++ }, toDisplayString(ctx.count), 1 /* TEXT */),
]));
}The Vue SFC Playground shows the real compiled output for any template.
Follow-up questions interviewers ask
Try answering each one before you open it.
What do v-once and v-memo do?
v-once renders an element or component once and treats it as static afterwards. v-memo="[a, b]" skips re-rendering a subtree unless a or b changes; it helps in very large v-for lists where most rows did not change (for example only the selected row changes).