Under The Hood
frontend vue Vue 3.x

Vue 3 Compiler PatchFlags: TEXT, CLASS, PROPS VNode Optimization

Last updated
Prerequisites:
Virtual DOM diffing algorithms
Bitwise operators in JavaScript
  • vue
  • compiler
  • patchflags
  • virtual-dom
  • optimization
  • vnode

Read at your depth

The practical view

PatchFlags are numeric bitwise hints generated by the Vue 3 template compiler. When an element has dynamic text (`<h1>{{ title }}</h1>`), the compiler emits `/* TEXT */ 1`. When an element has dynamic classes (`:class="cls"`), it emits `/* CLASS */ 2`. When the Virtual DOM diff runs, Vue uses fast bitwise operations (`patchFlag & PatchFlags.TEXT`) to diff *only* the specific dynamic attribute, completely skipping static props and children.

The same idea in other frameworks

react equivalent
Standard recursive Fiber reconciliation (no compiler-generated bitwise patch flags).

Legacy vs modern

Unoptimized Full VNode Diff vs Compiler PatchFlag Optimization

Unoptimized VNodes require iterating all props on every update; PatchFlags restrict diffing to exact bitwise targets.

before → after
Full Object Diff (Vue 2 / React)
createVNode('div', { id: 'fixed', class: dynamicClass }, text); // ❌ Diffs all props
Vue 3 PatchFlag Bitwise Optimization
_createVNode("div", { class: _normalizeClass(cls) }, _toDisplayString(msg), 3 /* TEXT, CLASS */); // ✅ Fast bitwise check

Interview gotchas

Context

Interviewers test your knowledge of compiler optimization boundaries and bailouts.

The mechanical answer

`PatchFlags.BAIL = -1` instructs the runtime to **bail out of optimized diffing** and fall back to full, unoptimized recursive tree diffing. This occurs when a template uses features that prevent static analysis (such as custom render functions, dynamic slots without static keys, or manual VNode injections).

Trap

Render functions written manually with `h()` default to full diffing unless you explicitly pass patch flag constants.

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

Third-party licenses Press ⌘ K to search.