DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on

Difference between Virtual DOM and Real DOM

DOM (Document Object Model) and Virtual DOM (Virtual Document Object Model) are both ways of representing the structure of a web page in a tree-like structure, but they differ in their implementation and purpose.

DOM is a programming interface for HTML and XML documents. It provides a way for programs to access and manipulate the elements, attributes, and content of an HTML or XML document. When a web page is loaded, the browser creates a DOM tree representing the structure of the page. The DOM tree can be manipulated through JavaScript, which allows for dynamic web pages.

On the other hand, Virtual DOM is a programming concept that aims to make updates to the DOM more efficient. Instead of updating the entire DOM tree when a change is made to a web page, the Virtual DOM creates a copy of the tree and compares it to the previous version of the tree. It then determines the difference between the two trees and updates only the parts that have changed. This process is faster and more efficient than updating the entire DOM tree.

The Virtual DOM is often used in front-end JavaScript frameworks like React to provide a high-performance user interface. When a user interacts with a React application, the Virtual DOM is updated to reflect the changes. The changes are then applied to the real DOM in a batch process, which reduces the number of expensive DOM operations and makes the application more efficient.

In summary, while DOM is a standard interface for interacting with web pages, Virtual DOM is a programming concept that is used to make updates to the DOM more efficient.

Real DOM

In web development, the Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the web page as a hierarchical tree structure, with each node representing an element, attribute, or text content of the page. The DOM provides a way for programs to manipulate the structure, style, and content of the web page.

The Real DOM, also called the "live" DOM, is the actual representation of the web page in the browser's memory. It's created by the browser when it parses the HTML document and builds the tree structure that represents the web page. When the DOM is updated, the browser re-renders the web page to reflect the changes.

The Real DOM is the basis for many web development techniques, such as dynamic updates and event handling. However, it can be slow and resource-intensive, especially when dealing with complex web pages or frequent updates. As a result, many web developers use techniques like virtual DOM and server-side rendering to optimize web page performance.

What is DOM

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the web page as a hierarchical tree structure, with each node representing an element, attribute, or text content of the page. The DOM provides a way for programs to manipulate the structure, style, and content of the web page.

In other words, the DOM is a programming interface that allows developers to interact with the HTML and XML elements of a web page. It provides methods and properties that can be used to access, modify, and create elements, as well as to add interactivity to the web page through event handling.

For example, if a developer wants to change the text of a button on a web page, they can use the DOM to access the button element and change its text content property. Similarly, if a developer wants to add an event listener to a button, they can use the DOM to register a function that will be called when the button is clicked.

Overall, the DOM is an essential part of web development, as it allows developers to create dynamic, interactive web pages that can respond to user actions and update in real-time.

Image description

Top comments (4)

Collapse
 
jegangits profile image
jegan

thanks

Collapse
 
vulcanwm profile image
Medea

great explanation!

Collapse
 
himanshudevgupta profile image
Himanshu Gupta

Thankyou

Collapse
 
eerk profile image
eerk

What would you say is the threshold where you decide you need to switch to a virtual DOM instead of using direct DOM manipulation?