Question presented to candidate: "People say Vue's virtual DOM is faster than a naive one because Vue has a compiler. What exactly does the template compiler do to make updates cheap? Talk me through static hoisting, patch flags and the block tree."
What a strong answer should cover:
- Templates are compiled ahead of time into render functions, so the compiler knows which parts can never change.
- Static hoisting / caching: static VNodes are created once and reused; in Vue 3.5 they are cached per instance in
_cache(flag-1 /* CACHED */), and static props objects are hoisted to module scope. - Patch flags: each dynamic node carries a bitmask (
1 TEXT,2 CLASS,8 PROPSplus the prop names,16 FULL_PROPS,128 KEYED_FRAGMENT, ...) so the renderer compares only what can change. - Block tree:
openBlock()/createElementBlock()collect dynamic descendants into a flatdynamicChildrenarray; updates walk that array instead of the whole tree. Structural directives (v-if,v-for) open new blocks. - Static stringification: long runs of static elements become one
createStaticVNodeHTML string. - Hand-written render functions get none of this.
Clarifying questions expected:
- "Do you want the concepts, or the actual compiled output?"
Code / implementation expected: Optional β compiled output for a small template, annotated.