Under The Hood
frontend vue Vue 3.x

Block Tree Optimization: Flattening Dynamic Nodes via openBlock & dynamicChildren

Last updated
Prerequisites:
Virtual DOM diffing
Vue 3 PatchFlags
  • vue
  • compiler
  • block-tree
  • openBlock
  • dynamicChildren
  • virtual-dom

Read at your depth

The practical view

The Block Tree is Vue 3's compiler technique to flatten the Virtual DOM. When compiling a template, Vue creates a **Block** root (`openBlock()`). Any nested element with dynamic bindings inside that block is tracked in a flat array called `dynamicChildren`. When updating, Vue **skips traversing static DOM elements completely** and iterates over the flat `dynamicChildren` array directly, reducing diffing work from `O(total nodes)` to `O(dynamic nodes)`.

The same idea in other frameworks

react equivalent
React compiler auto-memoizing JSX subtrees to bail out of fiber reconciliation.

Legacy vs modern

Deep Tree Traversal vs Block Tree Flat Array Iteration

Traditional VDOM traverses 100 nested static nodes to find 1 dynamic text binding; Block Trees diff only the 1 dynamic node.

before → after
Full Recursive Tree Traversal (Vue 2)
patchChildren(n1, n2); // ❌ Traverses 100 static divs to find 1 dynamic child
Vue 3 Block Tree (Flat Array Diff)
for (let i = 0; i < dynamicChildren.length; i++) patch(dynamicChildren[i]); // ✅ O(dynamic)

Interview gotchas

Context

Interviewers test your deep understanding of why flat arrays cannot safely span dynamic conditionals.

The mechanical answer

If a `v-if` conditional branch could be flattened into the parent block's `dynamicChildren` array, toggling the condition would change the length and order of the flat array, corrupting the index-based diffing of subsequent dynamic children. Therefore, `v-if` and `v-for` create **nested Block boundaries** (`openBlock()`), maintaining localized flat dynamic children arrays that can be cleanly swapped or keyed.

Trap

A template is not a single flat list; it is a **Tree of Blocks** where blocks are separated by `v-if`, `v-else`, and `v-for` boundaries.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.