React: DOM vs Virtual DOM

Vaishnavi Neema
2 min readSep 1, 2023

--

In React, the DOM(Document Object Model) and Virtual DOM are fundamentals concepts that play a crucial role in optimizing the performance of web applications. Here’s an overview of each concept:

  1. DOM(Document Object Model):
  • The DOM is a programming interface that represents the structure of an HTML or XML document as a tree of objects.
  • Each element in an HTML document, such as heading, paragraphs, buttons, and more, is represented as a node in the DOM tree.
  • When a web page is loaded or updated, the browser constructs the initial DOM tree based on the HTML markup.
  • Any changes to the content or structure of a webpage are reflected in the DOM, and these changes trigger a process called ‘reconciliation’ in which the browser updates the visible content on the screen.

2. Virtual DOM:

  • The Virtual DOM is a concept introduced by React to improve the efficiency of updating the actual DOM.
  • React creates an in-memory representation of the DOM called the virtual DOM. This representation is a lightweight copy of the real DOM.
  • When there’s a change in a React component’s state or props, React doesn’t immediately update the real DOM. Instead, it first updates the Virtual DOM to reflect the new state of the component.
  • After updating the Virtual DOM, React performs a process called ‘reconciliation’ to identify the differences between the old Virtual DOM and the new Virtual DOM.
  • Once the differences are identified, React calculates the most efficient way to update the real DOM to match the new Virtual DOM.
  • Finally, React applies the necessary changes to the real DOM, resulting in an update to user interface.

The key benefits of using the virtual DOM in React are:

  1. Improved performance: By minimizing direct, manipulation of the real DOM, React reduces the number of updates needed and increases the efficiency of the rendering process.
  2. Batched Updates: React can batch multiple updates together and apply them in a single pass reducing the number of expensive real DOM operations.
  3. Reusable components: Components in React are designed to be reusable and composable. The Virtual DOM helps manage the state and rendering of these components efficiently.
  4. Cross-Platform Compatibility: React’s Virtual DOM abstraction allows for easier integration with different rendering targets, such as web browsers and mobile apps(React Native).

In summary, React’s virtual DOM is a critical optimization technique that minimizes the performance overhead associated with frequent updates to the real DOM, making it a key feature for building efficient and responsive web applications.

--

--

Vaishnavi Neema
Vaishnavi Neema

Written by Vaishnavi Neema

Software Engineer, Learn Grow Earn Eat Repeat :)

No responses yet