As all articles say the Virtual DOM is lightweight and allows us to update only those nodes that has to be updated, unlike the real DOM that updates the whole nodes structure.
So the first question: how is Virtual DOM lightweight? Since it can't affect the nodes structure I assume VDOM does not have some methods that the real one has (like .createElement
). If I'm wrong then what make the VDOM lightweight?
Second question: how does VDOM make the real DOM update only necessary elements? Why does DOM not do that itself? Since VDOM can't affect the nodes structure it will eventually ask the real DOM do all the necessary updates. If the real DOM is able to update only necessary parts of the whole structure then why do we need the VDOM? Why isn't it a built-in DOM behaviour?
Some articles say that the VDOM accumulates updates and then rerender the whole DOM at once. While the real DOM updates the whole nodes structure right away everytime something changes the structure. So VDOM would change 5 element in one update while real DOM would need 5 updates to change 5 elements. If VDOM works like this then why does it traverse the whole structure and tag nodes that has to be updtaed? Why not just wait until there is no more changes to the structure and just update the whole DOM? Not traversing and tagging nodes?
Would really appreciate if you explain this to me in details or give me some links to in-depth articles/videos on this topic.