![[Vue][November 2024] - Introducing Vue’s latest experimental Vapor Mode](https://images.pexels.com/photos/546819/pexels-photo-546819.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1)
By Sean Erick C. Ramones, Vue SME | JavaScript/TypeScript SME
Sean Erick C. Ramones
By Sean Erick C. Ramones, Vue SME | JavaScript/TypeScript SME
Vue.js is renowned for its approachable syntax and efficient runtime. However, as performance demands increase for UI-heavy or real-time applications, Vue has introduced Vapor Mode. This experimental mode eliminates the virtual DOM (vDOM) and pre-compiles templates for faster rendering, making it ideal for high-performance scenarios.
| Feature | Default Mode | Vapor Mode |
|---|---|---|
| File Naming | .vue | .vapor.vue |
| Syntax | Options API / Composition API | Composition API |
| Virtual DOM | Uses vDOM for diffing and patching | No vDOM; pre-compiles templates directly to DOM operations |
| Rendering | Runtime template compilation | Direct DOM rendering (pre-compiled) |
| Reactivity | Fully reactive | Manually managed (if needed) |
| Performance | General-purpose, suitable for most apps | Optimized for performance-critical components |
Vapor Mode removes the vDOM and instead relies on pre-compiled, direct DOM manipulation for rendering. By bypassing the vDOM diffing process, Vapor Mode eliminates the overhead of reconciling the virtual and actual DOM, making it faster for specific use cases.
The syntax in Vapor Mode is almost identical to the Composition API. The key difference lies in the file extension (.vapor.vue) and the optimizations applied during the build process.
File: GraphComponent.vue
<template>
<svg :width="width" :height="height">
<polyline :points="points" style="fill: none; stroke: blue;" />
</svg>
</template>
<script setup>
const data = [10, 20, 30, 40, 50];
const width = 400, height = 200;
const points = data
.map((value, index) => `${index * 80},${200 - value * 3}`)
.join(' ');
</script>
File: GraphComponent.vapor.vue
// The very same code from above! Just with a different .vue extension name!
<template>
<svg :width="width" :height="height">
<polyline :points="points" style="fill: none; stroke: blue;" />
</svg>
</template>
<script setup>
const data = [10, 20, 30, 40, 50];
const width = 400, height = 200;
const points = data
.map((value, index) => `${index * 80},${200 - value * 3}`)
.join(' ');
</script>
Vapor Mode eliminates the vDOM and uses direct DOM rendering. With .vapor.vue files, templates are compiled directly to DOM operations during the build step, with no runtime diffing.
Default Mode and Vapor Mode can coexist within the same application. Use Default Mode for components requiring reactivity or dynamic behavior, and Vapor Mode for performance-critical components.
Example:
In most cases, Default Mode will remain our bread and butter—it’s reliable, flexible, and easy to use. Vapor Mode, on the other hand, is an exciting tool to explore for niche scenarios where performance is non-negotiable.
In the case for our project in Sysarb we could use Vapor components for the graphs since they are somewhat animation/data heavy as data-flow changes overtime. And the rest of the views (in this case the Dashboard ) we can use the regular default modes.
Vapor Mode is still in the experimental phase and is not yet merged into vuejs/core. However, the outlook for a release in 2024 looks good.
Happy coding! 🥲