Front-end engineers or anyone who tried to play with popular frontend libraries/frameworks like React and Vue.js, should all have heard about the Virtual DOM. It seems to have become a standard for improving performance in front-end development. However, there is also a trend that claims to not use the Virtual DOM. In this article, we will take a look at the Virtual DOM approach and discuss if Virtual DOM is outdated?
 represents the web document as nodes and objects; that way, programming languages can interact with the page.
Therefore, with DOM, we can easily work on the HTML nodes to create interactive components on a web page with Javascript and CSS.
DOM Tree Example
 DOM Revolutions
The Concept
As described above, since it costs so much for operating the DOM directly, what about to create a gateway that can be friendly to developers, at the same time it can help to reduce the unnecessary loads and efficiently reflect changes on DOM at pin-point level? To solve this, here comes our main character, the Virtual DOM.
The Virtual DOM is an light-weight abstract layer between the real DOM and the developers. It is actually a set of Javascript Objects with only the necessary elements and attributes to represent DOM tree and the nodes under it. With this abstract DOM tree, it becomes much easier and faster to create, edit or remove nodes just like the any normal Javascript Object.
Furthermore, with Virtual DOM, the rendering process is also abstract, so that it provides more compatibility for Cross-platform, not only for web browsers, but also mobile native components or other GUI developments.
The Approach: diff & patch
Many people got to know Virtual DOM via React, but in fact Virtual DOM is not invented by React. Many other libraries/frameworks are taking Virtual DOM approach. There are also many projects that only focus on Virtual DOM implementation such as virtual-dom and millionjs.
Although the way they implement Virtual DOM differs, the main approach is quite similar. There are two essential steps: diff and patch.
 {
var index = 0 // current node index
var patches = {} // record changes on each node
dfsWalk(oldTree, newTree, index, patches)
return patches
}
// depth-first walk through two DOM tree
function dfsWalk (oldNode, newNode, index, patches) {
// compare the changes and record by index
patches[index] = [...]
diffChildren(oldNode.children, newNode.children, index, patches)
}
// compare child nodes
function diffChildren (oldChildren, newChildren, index, patches) {
var leftNode = null
var currentNodeIndex = index
oldChildren.forEach(function (child, i) {
var newChild = newChildren[i]
currentNodeIndex = (leftNode && leftNode.count) // calculate node index
? currentNodeIndex + leftNode.count + 1
: currentNodeIndex + 1
dfsWalk(child, newChild, currentNodeIndex, patches) // traverse all child nodes
leftNode = child
})
}
Each Virtual DOM implementation may have its own algorithm to optimize the diff performance. Thanks to the efforts they have made, Virtual DOM became more and more mature, we don't even need to think about how it works during daily development.
Nevertheless, Virtual DOM is not the only solution for all.

Dongyu Wang
Frontend Engineer