What is the Difference between Virtual and Real DOM?

Topics Covered

Overview

In the realm of web development, terms like DOM (Document Object Model), Virtual DOM, and Real DOM are frequently thrown around. Grasping the difference between Virtual and Real DOM is crucial for developers, especially those working with frameworks like React. We'll examine these ideas in more detail in this article and discuss how they apply to web development.

What is DOM?

The Document Object Model (DOM) is a fundamental concept in web development. It represents the structure of an HTML document in a tree-like structure, where each element (e.g., <div>, <p>, <a>) is a node in the tree, and attributes, text, and child elements are properties of those nodes. The DOM allows JavaScript to interact with and manipulate the content, structure, and style of a web page dynamically.

Real DOM

The Real DOM, as the name suggests, is the actual, tangible representation of the web page's structure. Every time an element on a webpage is updated or modified, the Real DOM reflects these changes immediately. This means that if you were to change the text of a paragraph or move an image, the Real DOM would update itself to match these modifications.

Virtual DOM

In contrast to the Real DOM, the Virtual DOM is a lightweight, virtual representation of the Real DOM. It's essentially a copy of the Real DOM, but it exists in memory rather than being directly tied to the web page. When any changes are made to the web page, they are first reflected in the Virtual DOM instead of the Real DOM.

The Virtual DOM allows for batch updates and is more optimized for performance. When changes are made to the Virtual DOM, React (a popular JavaScript library for building user interfaces) compares it with the previous Virtual DOM snapshot, identifying the specific differences or changes made. This process is called reconciliation.

The Process of Updating in React

Understanding the difference between Virtual and Real DOM is especially crucial when working with React, as it utilizes this concept for efficient updates. Here's a simplified breakdown of how React updates a web page:

  • Initial Render :

    When you load a React application, it creates a Virtual DOM representation of the entire web page based on your components and their initial data.

  • User Interaction :

    When a user interacts with the application, such as clicking a button or filling out a form, changes are made to the Virtual DOM, not the Real DOM.

  • Reconciliation :

    React then performs a process called reconciliation. It compares the updated Virtual DOM with the previous one to identify the differences or changes made.

  • Minimal Updates :

    React calculates the most efficient way to update the Real DOM based on the differences in the Virtual DOM. It minimizes unnecessary updates, which can significantly improve performance.

  • Real DOM Update :

    Finally, React applies the calculated changes to the Real DOM, ensuring that the web page reflects the latest state.

Methods of DOM

In web development, the Document Object Model (DOM) provides a structured representation of an HTML document, allowing JavaScript to interact with and manipulate the content and structure of a webpage dynamically. To facilitate these interactions, the DOM provides various methods that allow you to access and modify elements within the DOM tree. Let's explore the methods you mentioned:

write("string")

This method is used to write HTML or text directly to the document. It can be useful for adding content dynamically. However, it's not commonly used in modern web development due to security and maintainability concerns.

Example

writeln("string")

Similar to write(), this method is used to write HTML or text directly to the document, but it also adds a line break (<br>) after the content.

Example :

getElementById("id")

This method allows you to retrieve an HTML element by its unique id attribute. It returns a reference to the first element with the specified id. This is one of the most commonly used methods for accessing elements in the DOM.

Example :

getElementByName("name")

This method is used to retrieve a collection of elements (usually form elements) with the specified name attribute. It returns an array-like object called a NodeList.

Example :

getElementsByTagName("tagname")

This method allows you to select elements by their HTML tag name. It returns a collection of elements with the specified tag name.

Example :

getElementsByClassName("classname")

This method is used to select elements by their CSS class name. It returns a collection of elements that have the specified class.

Example :

Difference between Virtual and Real DOM (Table)

AspectVirtual DOMReal DOM
Initial CreationCreated by JavaScript frameworks like React to track changes.Automatically created by the browser when a web page loads.
PerformanceMore efficient due to batch updates and minimal DOM manipulation.Can be slower due to direct manipulation and immediate updates.
Update EfficiencyCompares with the previous Virtual DOM snapshot and minimizes updates.Immediate updates reflect changes directly in the browser.
OperationsFaster for complex and frequent updates.Slower for complex and frequent updates.
Browser InteractionNo interaction with the browser's rendering engine.Direct interaction with the browser's rendering engine.
Synchronization UpdatesThe Real DOM selectively, reducing reflows and repaints.Updates all affected parts of the Real DOM, potentially causing reflows and repaints.
Use CaseCommonly used in JavaScript libraries/frameworks like React.Used in the core functionality of web browsers.

Conclusion

  • Virtual DOM is a lightweight, in-memory representation of the actual DOM, while the Real DOM is the tangible, actual representation of a web page's structure.
  • When it comes to performance, the Virtual DOM excels due to batch updates and minimal DOM manipulation.
  • In terms of update efficiency, the Virtual DOM compares changes with the previous snapshot and minimizes updates, while the Real DOM updates all affected parts directly.
  • Browser interaction for the Virtual DOM involves no direct interaction with the rendering engine, whereas the Real DOM directly interacts with the browser's rendering engine.
  • Synchronization with the Virtual DOM is selective, reducing reflows and repaints, while the Real DOM updates may lead to reflows and repaints.