React Virtual Dom vs Real Dom

In this article, we will understand why React uses Virtual DOM for better performance. We will also be comparing virtual dom vs real dom.

DOM stands for “Document Object Model,” which represents your application UI and whenever the changes are made in the application, this DOM gets updated and the user is able to visualize the changes. DOM is an interface that allows scripts to update the content, style, and structure of the document. Virtual DOM is a node tree similar to Real DOM that lists elements, content, and attributes as objects and properties. React render() method creates a node tree from the react components. Then it updates the node tree in response to the mutations in the data model caused by various actions done in the UI.

Let’s take a simple example of an array

let languages = [cpp, java, python]

Now we want to replace python with javascript. For that, we need to create a new array.

let languages = [cpp, java, javascript]

Instead of this, we can just traverse to languages[2] and update only the element. Instead of redoing the whole thing, we just changed the element which we needed to update. The same thing is done by Virtual DOM. Instead of updating all the node elements, it just updates the changed elements. Virtual DOM does the same thing. Instead of updating all the node elements, it just updates the changed elements. Virtual DOM does the same thing. Virtual DOM does the same thing. Instead of updating all the node elements, it just updates the changed elements.

ReactDOM.render() will create a Virtual and real DOM tree of the first load. When events like click, keypress, or API response occur, Virtual DOM tree elements are notified for state or prop change; if that state or props are updated, then the node elements are updated. When changes are done in UI, the changes are also done in Virtual DOM. Instead of updating all the nodes, Virtual DOM updates only those components in which changes are made.

Once Virtual DOM contains all the updated changes, it is then compared with the Real DOM and the difference is calculated between them.

Once Virtual DOM contains all the updated changes it is then compared with the Real DOM and the difference is calculated between them.

Virtual Dom vs Real Dom | Mindbowser

Once the difference is calculated the real DOM will update only the new components that have actually changed. This is called Reconciliation.  Virtual DOM is faster and more effective than Real DOM as it just focuses on the updated components instead of updating the entire DOM.

Real Dom | Mindbowser

coma

Summary

Virtual DOM plays a major role in boosting the performance of the applications. When the state of a component changes in an application, the Virtual DOM is updated and then compared with its previous state of the component. Only those few objects are changed in the real DOM that was the outcome of the comparison between the previous states and the new states. This feature made things move faster and also reduced cost. We hope you understood the comparison of virtual dom vs real dom and why React uses virtual dom.

Content Team

This blog is from Mindbowser‘s content team – a group of individuals coming together to create pieces that you may like. If you have feedback, please drop us a message on contact@mindbowser.com

Keep Reading

Keep Reading

Enhance Your Epic EHR Expertise in Just 60 Minutes!

Register Here
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?