The Wayback Machine - https://web.archive.org/web/20241010174028/https://www.geeksforgeeks.org/reactjs-virtual-dom/
Open In App

ReactJS Virtual DOM

Last Updated : 09 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

React JS Virtual DOM is an in-memory representation of the DOM. DOM refers to the Document Object Model that represents the content of XML or HTML documents as a tree structure so that the programs can be read, accessed, and changed in the document structure, style, and content.

What is DOM?

DOM stands for Document Object Model. In simple terms, it is a structured representation of the HTML elements that are present in a webpage or web app. DOM represents the entire UI of your application. The DOM is represented as a tree data structure. It contains a node for each UI element present in the web document. It is very useful as it allows web developers to modify content through JavaScript, also it being in structured format helps a lot as we can choose specific targets and all the code becomes much easier to work with.

Disadvantages of real DOM

Every time the DOM gets updated, the updated element and its children have to be rendered again to update the UI of our page. For this, each time there is a component update, the DOM needs to be updated and the UI components have to be re-rendered.

Example: 

// Simple getElementById() method
document.getElementById('some-id').innerValue = 'updated value';

When writing the above code in the console or in the JavaScript file, these things happen: 

  • The browser parses the HTML to find the node with this id.
  • It removes the child element of this specific element.
  • Updates the element(DOM) with the ‘updated value’.
  • Recalculates the CSS for the parent and child nodes.
  • Update the layout.
  • Finally, traverse the tree and paint it on the screen(browser) display.

Recalculating the CSS and changing the layouts involves complex algorithms, and they do affect the performance. So React has a different approach to dealing with this, as it makes use of something known as Virtual DOM.

What is Virtual DOM?

VirtualDOM in React is a lightweight copy of the actual DOM(a virtual representation of the DOM) used to optimize the updates on web page. So for every object that exists in the original DOM, there is an object for that in React Virtual DOM. It is exactly the same, but it does not have the power to directly change the layout of the document. 

Manipulating DOM is slow, but manipulating Virtual DOM is fast as nothing gets drawn on the screen. So each time there is a change in the state of our application, the virtual DOM gets updated first instead of the real DOM. 

understanding of React’s architecture and performance optimization techniques, consider enrolling in the React JS Course offered by GeeksforGeeks. This comprehensive course covers the fundamentals of React, including the Virtual DOM, component lifecycle, and state management. Through practical examples and hands-on projects, you’ll gain the skills necessary to build high-performance applications that leverage React’s powerful features.

How does virtual DOM actually make things faster?

When anything new is added to the application, a virtual DOM is created and it is represented as a tree. Each element in the application is a node in this tree. So, whenever there is a change in the state of any element, a new Virtual DOM tree is created. This new Virtual DOM tree is then compared with the previous Virtual DOM tree and make a note of the changes. After this, it finds the best possible ways to make these changes to the real DOM. Now only the updated elements will get rendered on the page again.

How virtual DOM Helps React?

In React, everything is treated as a component be it a functional component or class component. A component can contain a state. Whenever the state of any component is changed react updates its Virtual DOM tree. Though it may sound like it is ineffective the cost is not much significant as updating the virtual DOM doesn’t take much time. 

React maintains two Virtual DOM at each time, one contains the updated Virtual DOM and one which is just the pre-update version of this updated Virtual DOM. Now it compares the pre-update version with the updated Virtual DOM and figures out what exactly has changed in the DOM like which components have been changed. This process of comparing the current Virtual DOM tree with the previous one is known as ‘diffing’. Once React finds out what exactly has changed then it updates those objects only, on real DOM. 

React uses something called batch updates to update the real DOM. It just means that the changes to the real DOM are sent in batches instead of sending any update for a single change in the state of a component. 

We have seen that the re-rendering of the UI is the most expensive part and React manages to do this most efficiently by ensuring that the Real DOM receives batch updates to re-render the UI. This entire process of transforming changes to the real DOM is called Reconciliation.

This significantly improves the performance and is the main reason why React and its Virtual DOM are much loved by developers all around.

The diagrammatic image below briefly describes how the virtual DOM works in the real browser environment

Working of VirtualDOM

Real DOM to Virtual DOM

Virtual DOM Key Concepts

  • Virtual DOM is the virtual representation of Real DOM
  • React update the state changes in Virtual DOM first and then it syncs with Real DOM
  • Virtual DOM is just like a blueprint of a machine, can do changes in the blueprint but those changes will not directly apply to the machine.
  • Virtual DOM is a programming concept where a virtual representation of a UI is kept in memory synced with “Real DOM ” by a library such as ReactDOM and this process is called reconciliation
  • Virtual DOM makes the performance faster, not because the processing itself is done in less time. The reason is the amount of changed information – rather than wasting time on updating the entire page, you can dissect it into small elements and interactions

Differences between Virtual DOM and Real DOM

Virtual DOM Real DOM
It is a lightweight copy of the original DOM It is a tree representation of HTML elements
It is maintained by JavaScript libraries It is maintained by the browser after parsing HTML elements
After manipulation it only re-renders changed elements After manipulation, it re-render the entire DOM
Updates are lightweight Updates are heavyweight
Performance is fast and UX is optimised Performance is slow and the UX quality is low
Highly efficient as it performs batch updates Less efficient due to re-rendering of DOM after each update


Previous Article
Next Article

Similar Reads

Difference between Virtual DOM and Real DOM
DOM stands for Document Object Model it is the structural representation of the HTML Document. Real DOM is the actual structure represented in the User Interface while Virtual DOM is the memory representation of the same. It is a tree-like Structure consisting of all nodes in an HTML document DOM represents the Ul of your applications. Real DOMReal
3 min read
Mastering Efficient DOM Manipulation with Virtual DOM and Refs
Document Object Model (DOM) is used to create fast and efficient applications. It is used to manage and update the DOM in modern web development. When we try to directly change the DOM, it can be slow, especially when the website gets more complex. This is where tools like Virtual DOM and Refs come in use. They help make these updates faster and mo
6 min read
Creating Your Own Virtual DOM Using JavaScript
The Virtual DOM is a key concept in modern web development, particularly with libraries like React. It allows developers to efficiently update and render changes to the UI without re-rendering the entire DOM. In this article, we'll walk through creating a simple Virtual DOM implementation using plain JavaScript. Table of Content What is Virtual DOM
3 min read
What do you understand by Virtual DOM ?
The Virtual DOM is an in-memory representation of the DOM. DOM refers to the Document Object Model that represents the content of XML or HTML documents as a tree structure so that the programs can be read, accessed and changed in the document structure, style, and content. Prerequisites:DOMHTMLReact JS ReactDOMLet's see how a document is parsed by
3 min read
Replace a DOM element with another DOM element in place
The task is to replace an element by another element in place using JavaScript. Below are the few methods: parentNode Property: This property returns the parent node of the defined node, as a Node object. Syntax: node.parentNode Return value: It returns a node object, which represents the parent node of a node, or null if there is no parent. replac
3 min read
Dark Web - Crossing the virtual line
Introduction: Dark Web and the Deep Web have subsequently affected the society in either way, which we can say a good way and a bad way. If you cut an onion you will find many layers, if we put it in our real life It will seem like reaching to its core part is a little bit difficult for us. This thing describes the anonymity which is the core part
7 min read
Create a Virtual Machine and Set up API on Google Cloud
Cloud Computing is an attractive and prosperous field of Computer Science today. This introductory article focusses on how to work on Google Cloud to make computation, storage and working easier. Let's discuss the stepwise procedure to create a Virtual Machine and Setting up API on Google Cloud. At first, visit Google Cloud and select the Get Start
3 min read
How to detect virtual keyboard using JavaScript ?
In this article, we are given an HTML document, the task is to detect a virtual keyboard if it pops up on the device's screen. A virtual keyboard is a tool that helps in the input of characters without the use of the physical keyboard. It is widely used in touchscreen devices. Approach: Unfortunately, currently there is no direct way to detect if a
2 min read
Project Idea | Virtual Health Assistant
Introduction: One of the most common problems faced by today's people is a lack of knowledge of diseases and a lack of immediate first aid consultation. Due to this many people may suffer from physical and mental stress as they try to figure out the reason for their condition. In some cases, they even lost their lives and common diseases become lif
5 min read
Angular PrimeNG TreeTable Virtual Scrolling
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the TreeTable Virtual Scrolling in Angular PrimeNG. Angular PrimeNG TreeTable Virtual Scrolling is used to ena
5 min read
Angular PrimeNG Tree Virtual Scrolling
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Tree Virtual Scrolling in Angular PrimeNG. Angular PrimeNG Tree Virtual Scrolling is used to enable virtual scrol
5 min read
Mongoose Schema.prototype.virtual() API
The Mongoose Schema API .prototype.virtual() method of the Mongoose API is used on the Schema object. It allows us to define virtual method and type over each schema with a specific name and method definition. Let us understand virtual() method using an example. Syntax: schemaObject.virtual( <name>, <options> ); Parameters: This method
2 min read
Angular PrimeNG Form Dropdown Virtual Scroll Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Form Dropdown Virtual Scroll Component in Angular PrimeNG. The Virtual Scroll Component is used to create a v
4 min read
Angular PrimeNG Form MultiSelect Virtual Scrolling Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Form MultiSelect Virtual Scrolling Component in Angular PrimeNG. The Multiselect Component provides the u
4 min read
Build a Virtual Keyboard in Tailwind CSS
We will create a virtual keyboard with a responsive design, animated transitions, and colored keys using the Tailwind CSS. It will be responsive for all the devices. PrerequisitesHTMLTailwind CSSJavaScriptApproachUse Tailwind CSS for the styling and layout.Implement JavaScript for the keyboard functionality and animation.Create a virtual keyboard w
4 min read
Angular PrimeNG Form Dropdown Virtual Scrolling Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Form Dropdown Virtual Scrolling Component in Angular PrimeNG. The Virtual Scrolling Component is used to crea
9 min read
How to Setup Virtual Hosts with Apache Web Server on Linux?
Setting up a virtual host in Apache web server on Linux allows us to host multiple websites on a single server. This virtual host is useful when one wants to host multiple projects for businesses managing multiple domains with a single server. PrerequisitesApache server installed.Root or sudo accessSteps to set up virtual hosts with Apache web serv
2 min read
How to Create Virtual Whiteboard using JavaScript and Tailwind ?
The Whiteboard project aims to create a collaborative digital whiteboard application using the Tailwind CSS framework. This web application will allow users to draw, write, and collaborate in real time providing a virtual space for brainstorming, presentations, and creative collaboration. Output Preview: Let us have a look at how the final output w
5 min read
Design a Virtual Credit Card Template using HTML and CSS
In this article, we will design a Virtual Credit Card template using HTML and CSS for a modern and interactive user experience. This design includes spaces for the card number, name, and expiry date. HTML will be used to structure the layout, while CSS will add styling for a visually pleasing and better look. The virtual credit card design aims to
3 min read
Virtual Keyboard using React
In this article, we will create Virtual Keyboard using ReactJS. This project basically implements functional components and manages the state accordingly. Users can interact with the virtual keyboard by clicking on keys, and the interface will respond accordingly, allowing for a seamless typing experience. The keypress and character input logic are
7 min read
Build a Virtual Keyboard using HTML CSS & JavaScript
In this article, we will build a virtual keyboard using HTML, CSS, and JavaScript. A virtual keyboard is a user interface component that allows users to input text by clicking on the keys displayed on the screen. Our keyboard will support standard alphanumeric characters, special characters, and a Caps Lock feature. Preview of final output: Let us
4 min read
ReactJS Onsen UI AlertDialogButton Component
ReactJS Onsen-UI is a popular front-end library with a set of React components that are designed to developing HTML5 hybrid and mobile web apps in a beautiful and efficient way. AlertDialogButton Component denote each button of the alert dialog. We can use the following approach in ReactJS to use the Onsen-UI AlertDialogButton Component. AlertDialo
2 min read
ReactJS Rendering Elements
In this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered. What are React Elements?React elements are different from DOM elements as React elements are simple JavaScript objects and are efficient to create. React elements are the building b
3 min read
ReactJS MDBootstrap Spinner Component
MDBootstrap is a Material Design and bootstrap-based react UI library that is used to make good looking webpages with its seamless and easy-to-use component. In this article, we will know how to use Spinner Component in ReactJS MDBootstrap. Spinners are used to indicate the loading state of the component. Properties: tag: It is used to define the t
2 min read
ReactJS Evergreen Avatar Component
React Evergreen is a popular front-end library with a set of React components for building beautiful products as this library is flexible, sensible defaults, and User friendly. Avatar components are used to represent users only. We can use the following approach in ReactJS to use the Evergreen Avatar Component. Avatar Props: className: It is used t
2 min read
ReactJS Semantic UI Search Module
Semantic UI is a modern framework used in developing seamless designs for the website. It gives the user a lightweight experience with its components. It uses predefined CSS and JQuery to incorporate them into different frameworks. In this article, we will learn how to use the Search Module in ReactJS Semantic UI. The Search Module helps a user to
2 min read
ReactJS | Components - Set 2
In our previous article on ReactJS | Components we had to discuss components, types of components, and how to render components. In this article, we will see some more properties of components. Composing Components: Remember in our previous article, our first example of GeeksforGeeks's homepage which we used to explain components? Let's recall what
3 min read
ReactJS Props - Set 1
Till now we were working with components using static data only. In this article, we will learn about how we can pass information to a Component. What is props?React allows us to pass information to a Component using something called props (which stands for properties). Props are objects which can be used inside a component. Passing and Accessing p
5 min read
ReactJS Evergreen Autocomplete Component
React Evergreen is a popular front-end library with a set of React components for building beautiful products as this library is flexible, sensible defaults, and User friendly. Autocomplete Component is used for auto-completing the free text value with the option value. It basically allows the user to type and select the item from a list of options
3 min read
ReactJS Props - Set 2
In our previous article ReactJS Props - Set 1 we discussed props, passing and accessing props, passing props from one component to another, etc. In this article, we will continue our discussion on props. So, what if we want to pass some default information using props to our components? React allows us to do so. React provides us with something cal
4 min read