Return to site

โš›๏ธ REACT: Virtual DOM vs Real DOM

ยท react,fullstack

๐–๐ก๐š๐ญ ๐ข๐ฌ ๐ญ๐ก๐ž (๐ซ๐ž๐š๐ฅ) ๐ƒ๐Ž๐Œ?

The DOM is an abstraction of a pageโ€™s HTML structure.

It takes HTML elements and wraps them in an object with a tree structure.

This provides an API that allows us to traverse nodes (HTML elements) and manipulate them in several ways.

๐“๐ก๐ž ๐๐ซ๐จ๐›๐ฅ๐ž๐ฆ ๐ฐ๐ข๐ญ๐ก ๐ƒ๐Ž๐Œ

Given:

let fruits = ['Apple', 'Orange', 'Banana'];

Let's say we want to update the 'Orange' element with a 'Lemon' element.

Then we need to create a new array:

let fruits = ['Apple', 'Lemon', 'Banana'];

Efficiently, we can just traverse to the fruits[1] and update only this element.

Now itโ€™s common to have a thousand node in a single SPA.๐Ÿ˜ฑ

So repainting the whole page for each change is very very expensive.๐Ÿค‘

Ideally, weโ€™d like to only re-render items that receive updates, leaving the rest of the items as is.

๐“๐ก๐ž ๐Œ๐ž๐ญ๐ก๐จ๐๐ฌ ๐จ๐Ÿ ๐ฎ๐ฉ๐๐š๐ญ๐ž

1๏ธโƒฃDirty Checking (slow)๐ŸŒ

In AngularJS 1.x, data changes were checked by recursively traversing every node at fixed intervals, which was inefficient as it required examining all nodes even if their data was up-to-date.

2๏ธโƒฃObservable(fast)๐ŸŽ๏ธ

Components are responsible for listening when an update takes place.

Since the data is saved on the state, components can simply listen to events on the state and if there is an update, it can rerender to the UI.

React uses it.

๐•๐ข๐ซ๐ญ๐ฎ๐š๐ฅ ๐ƒ๐Ž๐Œ ๐Ÿ‘ป

The Virtual DOM is a lightweight ๐Ÿชถ abstraction of the DOM.

You can think of it as a copy of the DOM, that can be updated ๐Ÿ”„๏ธ without affecting the actual DOM.

It has all the same properties as the real DOM object but doesnโ€™t have the ability to write โœ๏ธ to the screen like the real DOM.

The virtual DOM gains its speed and efficiency from the fact that itโ€™s lightweight.

A new virtual DOM is created after every rerender.

๐”๐ง๐๐ž๐ซ ๐ญ๐ก๐ž ๐ก๐จ๐จ๐ โš™๏ธ

Reconciliation is a process to compare and keep in sync the two files (Real and Virtual DOM).

Diffing algorithm is a technique of reconciliation ๐Ÿค which is used by React.

Is Shadow DOM the same as the Virtual DOM?

No, they are different.

The Shadow DOM is a browser technology designed primarily for scoping variables and CSS ๐ŸŽจ in web components.

The virtual DOM is a concept implemented by libraries in JavaScript on top of browser APIs.

๐”๐ฉ๐๐š๐ญ๐ž ๐๐ซ๐จ๐œ๐ž๐ฌ๐ฌ ๐ข๐ง ๐‘๐ž๐š๐œ๐ญ

On the first load, ReactDOM.render() will create the Virtual DOM tree and real DOM tree.

As React works on Observable patterns, when any event(like the key press, left click, API response, etc.) occurred, Virtual DOM tree nodes are notified ๐Ÿšจ for props change.

If the properties used in that node are updated, the node is updated else left as it is.

React compares Virtual DOM with real DOM and updates real DOM.

This process is called Reconciliation ๐Ÿค.

React uses Diffing algorithm technique of Reconciliation.

Updated real DOM is repainted ๐Ÿ–Œ๏ธ on the browser.

#react #virtualDOM #programming

bit.ly/react2023

 

broken image