DEV Community

Shariq Ahmed
Shariq Ahmed

Posted on

DOM vs Virtual DOM — Key Differences!

DOM vs Virtual DOM — Key Differences

Are you ready to speed up your web development process If yes, then you first need to know what DOM and Virtual DOM is. But if you don’t know these terms, don’t fret. I’ll tell you everything in this article.

DOM

The term ‘DOM’ stands for Document Object Model. It is basically a tree-structured programming interface or API that can be used with any programming language. But, well it is most commonly used with JavaScript.

Now, whenever you click something on the web page, there’s a lot of work and changes made and done by the browser. One function that your browser performs if of updating the real DOM to show the changes. Afterward, your browser re-renders the whole page and shows the updated HTML. But the problem is that the real DOM is really slow. Because on every update, first the complete document layout is calculated. But there’s this one advantage of using DOM. You can directly edit and change HTML. In fact, if elements are updated then a new DOM is created.

Further, real DOM also has nodes in HTML documents. It shows the UI of the application. But since nowadays, most websites are SPA, DOM trees must be changed multiple times. And this is really time-consuming.

Virtual DOM

To solve these problems, React uses Virtual DOM that’s basically a copy of real DOM. It’s also lightweight.

What’s even better? Virtual DOM updates quickly. It also provides better performance because here React first updates the Virtual DOM, and only updates the Real DOM that requires changes after comparing it with the previous DOM. This whole process is termed reconciliation.

Virtual DOM is fast because instead of every node, event listeners are placed on only the root node. Another best thing? Virtual DOM can be used with any programming language that uses JavaScript. DOM manipulation is also easy. But in Virtual DOM, HTML can’t be updated directly. Also, contrary to the real DOM, in Virtual DOM when elements are updated, JSX is updated.

And that’s it. You now know the difference between real and virtual DOM. I’ve also explained to you how you can speed up your website development. So, tell us what are you planning to use: real or virtual DOM?

Top comments (0)