• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    JavaScript
  • Created almost 7 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👉 A minimal implementation of the Virtual DOM

v-dom

👉 A minimal implementation of the Virtual DOM

What's a Virtual DOM

Let's talk about Real DOM first.

DOM manipulation is the heart of web development. Five years before, jQuery was popular because it wraps up a set of useful DOM manipulation functions and css-like selectors, which are more intuitive and easier to use. However, later, we began to value more the cleaness and maintainability of front-end code. And the spegatti style of jQuery doesn't fit into what we need. Then we have many even higher level frameworks like React, Vue, Angular, etc. Under the hood of which, they are actually building a even higher level of abstraction over DOM manipulation, which saves the pain of calling DOM API directly.

The so-called term "Virtual DOM" was first brought by developers from React. Behind the fancy name, the idea is actually quite simple. DOM manipulation is expensive, but operating native types like object, array is quick. So in React we have a the mapping between real DOM and react element, which is nothing more than a JavaScript object. Thus we represent the DOM structure with an object tree. This is what Virtual DOM is all about.

To build the simplest form of Virtual DOM, we care only two aspects: Mounting and Updating.

Mount

Mounting is the process of take the element as input and create the real DOM node. The type field of each element indicates what kind of element it is. And since the children attribute itself is a list of element, it enables the recursion in the process of mounting.

Patch

Compared to mounting, patching is more complicated. Inside patch, we have two sub processes.

The first one is diffing, aka the Virtual DOM Diff algorithm. Here is where the JavaScript native object comes in to the rescue because it's so much faster than comparing actuall DOM nodes. By comparing the element tree, we generate the operations sequence that we need to perform, aka patches.

Using these operation sequence, which are generally an array, we iterate on it and perform real DOM update functions, for example, we use document.createElement, appendChild, replaceChild, to do CREATE, REPLACE, and if the element stays the same, we go deep into its children property to perform diff and patches recursively.

More Repositories

1

learn-react-source-code

Build react from scratch (code + blog)
JavaScript
208
star
2

css-filter

Upload an image and add filters to it. Pure CSS, no black magic.
JavaScript
99
star
3

image-screenshot

download an image node along with its css properties
JavaScript
54
star
4

zion

[Deprecated] A tentative, light-weight 2D game framework based on HTML5 canvas.
JavaScript
44
star
5

Migration-to-Suspense

Slides and demo of D2 Forum in Jan. 2019
36
star
6

tetris-redux

🏆 A React / Redux implementation of the Tetris game.
JavaScript
29
star
7

canvas-image-cache

A promise-based utility to cache images in canvas to enhance rendering efficiency and prevent flickering
JavaScript
14
star
8

react-suspense-demo

Use React Suspense today
JavaScript
11
star
9

react-animation-wrapper

🔮 Components animation wrapper done right
JavaScript
11
star
10

leetcode

💫 Python / JavaScript solutions for LeetCode
Python
9
star
11

zakas

🤖 A desktop Siri-like voice manager bot, to automate your daily routine.
Python
7
star
12

til

✨ Curated list of useful code snippets / programming tips & knowledge. Check my Gists as well!
Shell
6
star
13

memoize

A JavaScript function wrapper to memorize / cache result with the same input
JavaScript
6
star
14

tetris-mobx

A React / MobX implementation of the Tetris game.
JavaScript
6
star
15

React-Kanban-Board

📋 A checklist SPA implemented with React
JavaScript
5
star
16

easy-stackoverflow

🙊A Chrome extension for StackOverflow
JavaScript
5
star
17

pubsub

🔮 The JavaScript implementation of the publish/subscribe pattern.
JavaScript
5
star
18

react-provider-standalone

The standalone React components <Provider /> wrapper.
JavaScript
5
star
19

node-online-snake

The html5 snake game implemented with Express.js and Socket.io
JavaScript
5
star
20

dumb-state

A simple state management tool that supports undo, redo, etc.
JavaScript
4
star
21

handwriting

📖 A web application to practice your handwriting.
JavaScript
4
star
22

focus-helper

A chrome extension to help you boost your focus in a period of time
4
star
23

hands-on-react-hooks

A handful of custom React hooks, mostly for education purpose.
3
star
24

sane-plural

get the plural form in a sane way
JavaScript
3
star
25

try-infinitely

a simple helper function to make an error-prone call infinitely / or until some value is truthy
JavaScript
3
star
26

tiny-react-ui

A lightweight, easy-to-use ui component kit for React
3
star
27

Backbone-Todo-List

The todo list based on Backbone.js
HTML
3
star
28

greedy-snake

A simple html5 game aimed at children aged above 3. LOL
HTML
3
star
29

keybus

🏄 Support simultaneous multi-keypress handler. Especially useful in game development on web browser.
JavaScript
3
star
30

Front-End-Data-Visualization

👀 Web Data Visualization for the graduate thesis in 2017.
JavaScript
3
star
31

hook-useEffect-repro

[This is a wrong usage example]
JavaScript
2
star
32

react-undo-redo

An Higher Order Component that gives you ability to undo / redo state changes.
2
star
33

mini-observer

👀 A redux-like implementation of the observer pattern in JavaScript
JavaScript
2
star
34

express-socket.io-boilerplate

An un-opinionated, minimal boilerplate to get started with Express.JS and Socket.io
HTML
2
star
35

weather-cli

☁️ A Node.js Command Line Tool to Know the Weather Data
1
star
36

AsyncImage

Lazy load image with React suspense
1
star
37

sponsibly

http://cyan33.github.io/sponsibly
HTML
1
star
38

react-collapsed-image

JavaScript
1
star
39

Data-Structure-and-Algorithms-in-JavaScript

A Minimal Implementation of the Common-Seen Data Structure and Algorithms in JavaScript.
JavaScript
1
star
40

fast-scroll-top

A Chrome extension to save your pain of scrolling back to the top of the page.
JavaScript
1
star
41

codec

📠 A light weight codec implemented in JavaScript, aimed for both browser and node.js
JavaScript
1
star
42

is-equal

a JavaScript function utility to check if two things are content-wise equal or not
JavaScript
1
star
43

naming-transformer

Switching between variable naming conventions like camel, snake, and general title
JavaScript
1
star