why updating the Real DOM is slow, what is Virtaul DOM, and how updating Virtual DOM increase the performance?

Updating a DOM is not slow, it is just like updating any JavaScript object; then what exactly makes updating Real DOM slow?

Updating a DOM is not slow, just like updating any JS object, so what makes updating the DOM slow?

 

 

 

Rendering engines which is responsible for displaying or rendering the webpage on the browser screen parses the HTML page to create DOM. It also parses the CSS and applies the CSS to the HTML creating a render tree, this process is called as attachment.

The Rendering engine is responsible for displaying and rendering web pages. parse parses the HTML page syntactically to create the DOM. It also lexically analyzes the CSS and applies the CSS to the HTML to create a rendertree. This process is called attachment.

 

So when we do,

document.getElementById('elementId').innerHTML = "New Value"

Following thing happens:

  1. Browser have to parses the HTML
  2. It removes the child element of elementId
  3. Updates the DOM with the “New Value”
  4. Re-calculate the CSS for the parent and child
  5. Update the layout i.e. each elements exact co-ordinates on the screen
  6. Traverse the render tree and paint it on the browser display

Recalculating the CSS and changed layouts uses complex algorithm and they effect the performance.

 

When assigning a value to innerHTML, the following things happen:

1. Browser lexical analysis of HTML

2. Delete the child elements of element

3. Update the DOM with new value

4. Recompute CSS for parent and child elements

5. Accurate coordination update layout

5. Go through the entire rendertree and draw in the browser

Recalculating CSS and layout uses complex algorithms and affects performance

 

Thus updating a Real DOM does not involves just updating the DOM but, it involves a lot of other process.

Also, each of the above steps runs for each update of the real DOM i.e. if we update the Real DOM 10 times each of the above step will repeat 10 times. This is why updating Real DOM is slow.

Therefore, updating a real DOM not only updates the DOM, but also affects many other processes.

At the same time, each time the real DOM is updated, the above process will run once. If we update the real DOM ten times, the above steps will be repeated 10 times, which is why updating the real DOM is very slow.

 

How Virtual DOM solves this problem?

How does virtual DOM solve this problem?

 

What is virtual DOM?

What is virtual DOM?

Virtual DOM is in-memory representation of Real DOM. It is lightweight JavaScript object which is copy of Real DOM.

Updating virtual DOM in ReactJS is faster because ReactJS uses

  1. Efficient diff algorithm
  2. Batched update operations
  3. Efficient update of sub tree only
  4. Uses observable instead of dirty checking to detect change

 

The virtual DOM is the identification of the real DOM in memory, and it is a very lightweight JS object (wichi is copy of Real DOM)

Updating the virtual DOM is faster in ReactJS because ReactJS uses

   1. Effective diff algorithm

   2. Update operations in batches

   3. Only valid updates to subtrees

   4. Use observables instead of dirty checks to monitor changes

 

AngularJS uses dirty checking to find the models which has changed. This dirty checking process runs in cycle after a specified time. As the application grows, checking the whole model reduces the performance and thus makes the application slow.

AngularJS uses dirty checking to find where the models have changed. The dirty checking is performed in a loop at a certain time. When the app grows, checking the entire model can cause performance degradation.

 

ReactJS uses observable’s to find the modified components. Whenever setState() method is called on any component, ReactJS makes that component dirty and re-renders it.

ReactJS uses observables (observables?) to find modified components, and when setState() is called on a component, ReactJS makes that component dirty and redraws it.

 

Whenever setState() method is called, ReactJS creates the whole Virtual DOM from scratch. Creating a whole tree is very fast so it does not affect the performance. At any given time, ReactJS maintains two virtual DOM, one with the updated state Virtual DOM and other with the previous state Virtual DOM.

ReactJS using diff algorithm compares both the Virtual DOM to find the minimum number of steps to update the Real DOM.

Finding minimum number of modifications between two trees have complexity in the order of O(n^3). But react uses heuristic approach with some assumptions which makes the problems to have complexity in the order of O(n).

ReactJS creates the entire Virtual DOM when setState() is called. Creating a tree is very fast and doesn't affect performance very much. At any time, ReactJS maintains two virtual DOMs, one for the updated state, and one for the updated state. is the virtual DOM of the previous state

ReactJS uses the dff algorithm to compare two virutalDOMs to find the least number of steps to update the real DOM

The time complexity of finding the minimum number of modifications between two trees is O(n^3), but React has developed some heuristic strategies to reduce this complexity to O(n)

 

ReactJS uses following steps to find the difference in both the Virtual DOM’s

ReactJS uses the following steps to find the difference between two Virtual DOMs

    1. Re-render all the children if parent state has changed. If the state of a component has changed, then ReactJS re-renders all the child components even if child components are not modified. To prevent the unwanted re-render of the child components we can use shouldComponentUpdate() component life cycle method. This will further help in boosting the performance.
    2. Breadth First Search. ReactJS traverse the tree using BST. Consider the below tree. States of element B and H have changed. So when using BST ReactJS reached element B it will by default re-render the element H. This is the reason to use BST for tree traversal
    3. Reconciliation. It is the process to determine which parts of the Real DOM need to be updated. It follow below steps:

        1. Two elements of different types will produce different trees.
        2. The developer can hint at which child elements may be stable across different renders with a key prop.

      1. If the parent's state changes, re-render all children. If a component's state changes, ReactJS repaints all child components even if the child components haven't changed. To prevent unnecessary re-renders we can use shouldComponentUpdate(), which can greatly help improve performance.

       2. Breadth-first search, ReactJS uses breadth-first search to traverse the tree, consider the following tree, the states of B and H have changed, when using breadth-first search, ReactJS will re-render element H by default when it arrives at B, which That's why you use breadth-first traversal.

     3. Consistency comparison. This step determines which part of the real DOM will be updated. It follows the steps below:

        1. Two different types of elements produce different trees

         2. Through key prop developers can hint those elements that are still stable in different renders

 

Batch Update

ReactJS using the diff algorithm to find the minimum number of steps to update the Real DOM. Once it has these steps, it executes all the steps in one event loop without involving the steps to repaint the Real DOM. Thus, if there are more element which gets updated ReactJS will wait for the event loop to finish then, in bulk will updated the real DOM with all the updated elements.

Once all the steps are executed, React will repaint the Real DOM. This means during the event loop, there is exactly one time when the Real DOM is being painted. Thus all the layout process will run only on time for updating the real DOM.

 

 

batch update

ReactJS uses a diff algorithm to find the smallest steps to update the real DOM. Once it has the steps, it executes all of them in an event loop (without triggering a DOM repaint), so if there are a lot of elements that need to be updated, ReactJS processes these events through the event loop, in batches Update these real DOMs that need to be updated.

 

Once all steps are complete, React will redraw the virtual DOM, which means that in the event loop, the real DOM will only be drawn sequentially, so all layout processing will only run once in order to update the real DOM.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324866283&siteId=291194637