DEV Community

Cover image for Virtual DOM in React

Virtual DOM in React

Deepansh Bhargava
Full Stack Javascript Developer
・3 min read

Today we gonna learn about the Virtual DOM present in React, which is responsible for its fast performance and declarative nature.

What is DOM?

DOM stands for Document Object Model, which represents the node tree of all the UI elements present on a page. If any element on the UI changes, the DOM updates itself with the new changes.

This process of updating the DOM frequently makes the web page slow, so that's why there was a need for Virtual DOM. As we know DOM represents Tree Structure so the changes and updates are done quickly but the rerendering part takes a long time to complete.

If you know the Critical Rendering Path, the last step of it i.e Paint takes the longest time to complete, that's why browsers are optimized to repaint in minimum time.

Critical Rendering Path


What is Virtual DOM?

Virtual DOM represents a copy of the actual DOM. React maintains a copy of the DOM and renders the UI according to this DOM.

React works on the concept of observable pattern. It listens to all the changes made in the actual DOM and updates the virtual DOM only with that changes. React then renders the UI only with the changes that happened in the virtual DOM. This process is called diffing.

The process of maintaining sync between the actual DOM and the virtual DOM is called reconciliation.

How Virtual DOM is faster?

As soon as any update is done in the actual DOM, the reconciliation algorithm marks the element as dirty, and goes on to process the next update. When all the updates are processed in the DOM, they are passed to the virtual DOM in batches, instead of one by one as we have in the actual DOM. This makes the virtual DOM a lot faster and thus makes React performant.

Keys - A solution of rerenders

The reference of a UI element in the actual DOM and in the Virtual DOM need not be the same. So what happens in this case, the react will rerender everything even though we changed a single line.

Suppose we have this piece of code. The DOM will render it fully as it's the first time.

  <p>Hello! I am a piece of text.</p>
  <p>Don't underestimate me!</p>
Enter fullscreen mode Exit fullscreen mode

Now, if we add another paragraph the DOM will render again all the unchanged paragraphs too.

  <p>Hello! I am a piece of text.</p>
  <p>Don't underestimate me!</p>
  <p>You don't know my powers!</p>
Enter fullscreen mode Exit fullscreen mode

Now you'll be like why React is not behaving as it's supposed to be.

So here as I said the reference need not be the same, the first two paragraphs in the actual DOM and in the virtual DOM are different for React and it's considering them as new additions to the DOM. This lead to rerendering of the DOM unnecessarily. This problem can be solved with the help of keys.

  <p key="1">Hello! I am a piece of text.</p>
  <p key="2">Don't underestimate me!</p>
Enter fullscreen mode Exit fullscreen mode

While rendering lists, generally we give keys to the element and for many developers, it's a headache to decide what will be the key. So to solve this problem I stumbled upon this syntax of React.

  {React.Children.toArray( => <p>{text}</p>))}
Enter fullscreen mode Exit fullscreen mode

That's it for now folks. Thanks for reading this article. Let me know if anything is not clear.

Connect with me on Twitter, Instagram & LinkedIn

Happy Coding!

Discussion (2)

lexlohr profile image
Alex Lohr

Another solution is not to create a new node every time, but store the nodes directly, especially when they're static anyways:

// Don't
const MyStaticFC = () => <div>...</div>

// Do
const MyStaticNodes = <div>...</div>
Enter fullscreen mode Exit fullscreen mode
deepansh946 profile image
Deepansh Bhargava Author

Yeah agreed.