First and very important, let’s understand the DOM:
DOM (Document Object Model)
It’s the object model used in a web document, or so to speak, a web page. It defines how is modeling the objects that make up the page, things for example that the root of the document is the <html>
, who has as children for example <head>
and <body>
, that within the <body>
we use elements like <p>
and <div>
, that the tag <td>
should be nested and daughter of a tag <tr>
, who in turn is a daughter of a tag <table>
, and so on.
Understanding this, let’s see the Virtual and Shadow DOM.
Virtual DOM
It is a copy or clone of the DOM itself, or in a simple way, a copy of the objects on the page.
Its purpose is very simple: improve performance in DOM manipulation.
You may not have noticed, but a page with many elements in the DOM can get physically large, and it has a crucial detail when it comes to performance: changes in the DOM can make the whole page render, and some processes made by Javascript can block the updating of the page.
This means that a simple color change or something more "heavy" like hiding a div that forces you to reposition adjacent div may require a lot of processing and time.
This is where the Virtual DOM comes in: its main object is to make all the changes in this "mirror" of the real DOM and apply everything at once, thus winning in performance. This way, you can update everything you need in an optimized way.
Shadow DOM
It is related to the concept of encapsulation of the Web components. Its main focus other than Virtual DOM is not performance related to changes in the DOM, but to make the code more readable, transparent and easy to understand. For example: knew the tag <input>
is formed internally by elements <div>
?
The following image shows this (I enabled the Shadow DOM view in Chrome’s Dev Tools), note the Node #shadow-root
:
Note that, instead of creating a two Divs to position where the content will be typed, it is simpler and transparent to use a tag called <input>
.
Some Frameworks make good use of this concept of componentization, such as Angular
, where this is done through directives.
Now it is clear to understand the difference between the two and what the main purpose of each is.
On the question, the manipulation of Virtual DOM is clearly more focused on performance, and Shadow DOM on simplifying the reading of the document and its semantics.
One important thing to note is that the Virtual DOM needs to be implemented, created with your own code if you don’t use any Framework already using it, while the Shadow DOM is already implemented by browsers, here you can see an example of code for how to create a pseudo-element: Using Shadow DOM
Here are some links from sources where I searched for information and also some for reference:
- Reference of the FOD: DOM Reference
- Shadow DOM Reference: Using Shadow DOM
- Shadow DOM compatibility: Caniuse?
- On line benchmarks (DOM, Virtual DOM and others), compare the rendering/document modification performance: uibench
Related: What is the difference between gift and virtual gift
– Denis Rudnei de Souza
@Denisrudneidesouza I would like a more specific answer regarding the Shadow DOM effectively, would know to help me?
– Getulio Rafael Ferreira
Oops, I put as related more to help other people, I’m also seeing a good answer to that
– Denis Rudnei de Souza
It’s more like a compliment
– Denis Rudnei de Souza
Shadow DOM and Virtual DOM have no relationship. A framework can use both or none. Shadow DOM is an encapsulation of the node tree, while Virtual DOM is an abstraction of DOM for DOM to be manipulated efficiently. You can create an answer with the advantages of each one, but you can’t compare one "against" the other.
– fernandosavio
What is Shadow DOM?
– Woss