tezvyn:

Vue's Virtual DOM: Efficient UI Updates

AI-drafted, machine-checkedSource: vuejs.orgbeginner

Vue keeps a JavaScript copy of the UI (the Virtual DOM) in memory. When data changes, it compares a new virtual tree to the old one and efficiently updates only the changed parts of the real DOM, avoiding costly full re-renders.

WHY IT EXISTS Manipulating the browser's DOM directly is slow and complex. Every change can trigger expensive re-layouts and re-paints that degrade performance. Frameworks needed a way to manage UI updates predictably and efficiently without the developer having to manually optimize every DOM interaction.

THE MENTAL MODEL Think of the Virtual DOM as an architect's blueprint for a building. Instead of demolishing and rebuilding the entire structure for a small change (like moving a window), you first update the blueprint. Then, you compare the new blueprint to the old one, identify only the window that moved, and send a specific instruction to the construction crew to change just that part. The real DOM is the building; the VDOM is the blueprint.

HOW IT WORKS Vue's process has three main steps. First, during the 'compile' step, Vue templates are converted into render functions. These are JavaScript functions that, when executed, produce a tree of virtual nodes (vnodes). Second, during the initial 'mount', the renderer executes the render function, gets the VDOM tree, and builds the corresponding real DOM elements. This process is reactive, tracking which data was used. Third, during 'patch', if any of that tracked data changes, the render function re-runs, creating a new VDOM tree. The renderer then performs a 'diffing' algorithm, comparing the new and old trees to find the minimal set of changes. It then applies only these changes to the real DOM, which is far more efficient than recreating everything.

WHEN TO USE IT The VDOM is Vue's core rendering mechanism, so you're always using it when writing templates. It excels in applications with dynamic UIs where parts of the view change frequently in response to user actions or data fetching. It abstracts away the complexity of direct DOM manipulation, letting you declaratively describe your UI.

WHEN NOT TO USE IT You don't choose to disable the VDOM when working with Vue components. However, for extremely simple, static web pages, the overhead of a framework like Vue might be unnecessary. For highly specialized, performance-critical tasks like high-frequency canvas animations, you might bypass the framework and interact with the rendering context directly for maximum control.

ONE CANONICAL EXAMPLE A vnode is a plain JavaScript object representing an element. For a with an ID, it looks like this: const vnode = { type: 'div', props: { id: 'hello' }, children: [] }. When this vnode is part of a tree, the renderer walks it and creates an actual element in the browser.

Read the original → vuejs.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.