Skip to content

Rendering Mechanism

Vue compiles templates into efficient JavaScript render functions. Learn how the virtual DOM and reactivity system work together to update the UI efficiently.

  1. Template Compilation
    Vue templates are compiled into JavaScript render functions. These functions return virtual DOM trees describing what should be rendered.

  2. Virtual DOM
    The virtual DOM is a lightweight JavaScript representation of the actual DOM. When state changes, Vue creates a new virtual DOM tree and efficiently compares it to the previous one (diffing).

  3. Patching
    Vue calculates the minimal set of changes needed and updates only the affected parts of the real DOM (patching), improving performance.

  4. Reactivity System
    Vue tracks dependencies during rendering. When reactive data changes, only the components that depend on that data are re-rendered.

<template>
<p>{{ message }}</p>
</template>
<script setup>
import { ref } from "vue";
const message = ref("Hello Vue!");
</script>

When message changes, Vue automatically re-renders only the affected part of the DOM.

  • Use key with v-for for efficient list updates.
  • Avoid unnecessary reactive dependencies in templates.
  • Use computed properties for expensive calculations.