Introduction to Snabbdom and getting started

Learn what the virtual DOM is, how Snabbdom relates to Vue 2, and how to render your first virtual DOM example.

🎯 Introduction

By the end of this article, you will understand what the virtual DOM is, what Snabbdom does, and how it relates to Vue 2. We will also set up a practice project and render our first virtual DOM in fewer than 20 lines of code.


🧱 What is the virtual DOM?

A page consists of DOM elements. Working with the DOM directly has two drawbacks: the code can be tedious to write, and frequent changes can trigger repeated browser layout and painting, which can be expensive.

The virtual DOM describes a DOM structure using JavaScript objects. Consider this real div:

<div id="app">Hello</div>

Its virtual DOM representation is an ordinary JavaScript object:

const vnode = {
    sel: 'div#app',
    text: 'Hello'
};

Think of it as a blueprint: the real DOM is the finished house, and the virtual DOM is the plan. To make a change, draw a new plan, compare it with the old one, and change only the parts of the house that differ, rather than rebuilding the whole house.


🧱 What is Snabbdom?

Snabbdom is a JavaScript library focused on the virtual DOM. Its name comes from Swedish and means "quick" or "agile." It has two defining characteristics:

  • Small: its core is only about 200 lines, with little historical baggage, making it well suited to learning.
  • Modular: the core handles creating and comparing virtual nodes. Styles, attributes, events, and other features are loaded as modules as needed.

Most notably, Vue 2's virtual DOM implementation was adapted from Snabbdom. Learning Snabbdom therefore gives you a foundation for understanding how Vue 2 works internally.

Vue 3 has its own virtual DOM implementation, but shares the same core ideas: virtual nodes, diffing, and keys. This course will also help you understand Vue 3.

🚀 Set up the practice project

This course uses Node.js v22.0.0 and Snabbdom 3.6.4.

The virtual DOM ultimately needs to render in a browser. We will create a minimal project with Vite, which we covered in Stage 4:

npm create vite@latest snabbdom-demo -- --template vanilla
cd snabbdom-demo
npm install
npm install snabbdom@3.6.4

Start the development server:

npm run dev

Open the URL shown in your terminal, usually http://localhost:5173. If you see the Vite welcome page, your environment is ready.


📦 First example: render and update a page

Open main.js, remove the template code, and add:

main.js
import { init, h } from 'snabbdom';

// init creates the patch function. Start with an empty array; article 3 explains it.
const patch = init([]);

// Use h to create a virtual node: a div with the id container.
const vnode = h('div#container', 'Hello Snabbdom');

// Render the virtual node on the page.
const container = document.getElementById('app');
patch(container, vnode);

You need a mount point in index.html (the vanilla template already includes one):

index.html
<div id="app"></div>

Save the file, and you should see Hello Snabbdom in the browser. The page structure is now:

<div id="container">Hello Snabbdom</div>

Next, try an update. Append this code:

main.js
// Create a new blueprint, compare it with the old one, and update the page.
const newVnode = h('div#container', 'Hello, virtual DOM');
patch(vnode, newVnode);

The text immediately changes to "Hello, virtual DOM". Notice that the second call to patch receives the previous vnode and the new vnode. This is the comparison between the old and new blueprints.

This code introduces three core functions. Remember their names for now; later articles cover each in detail:

FunctionPurpose
hCreate virtual nodes (draw the blueprint)
initCreate the patch function and optionally register modules
patchCompare nodes and update the real DOM (build from the blueprint)

❓ Questions and answers

Q1: Is the virtual DOM always faster than direct DOM manipulation?

No. Its value lies in using JavaScript computation to minimize DOM operations while improving the developer experience. On the initial render, it actually adds computation. It is no silver bullet, but can be very useful for interfaces whose data changes frequently.

Q2: Can I use Snabbdom directly in a real project?

Yes, but it is a low-level library usually integrated into frameworks such as Vue 2. For everyday development, we more often use Vue or React. The main reason to study Snabbdom here is to understand the underlying principles.

Q3: Why not load Snabbdom with a script tag?

Snabbdom 3.x is distributed as ES modules. A development tool such as Vite makes it easy to use and automatically refreshes the page when you save, making practice more convenient.

Q4: What does the name h mean?

h stands for hyperscript: writing HTML using a script, in this case JavaScript. It is a conventional name in virtual DOM libraries.


🧪 Exercise

  1. Follow the steps in this article to create snabbdom-demo and install snabbdom@3.6.4.
  2. Edit main.js to render a section with the id box and the text "My first virtual node". Then update its text to "Content updated".
main.js
import { init, h } from 'snabbdom';

const patch = init([]);

// Write your code here.

🎉 You have completed your introduction to Snabbdom! Next, we will explore the h function and see how it describes more complex page structures.