The Reasons Behind React's Fast Performance

The Reasons Behind React's Fast Performance

React is a JavaScript library used by many developers, both junior and senior. React has become extremely popular over the years because of its blazing-fast performance. But many developers might not know the reason behind this speed. I will demystify React in this article and uncover what makes it so fast. Let's begin! Let us begin with what reconciliation is. In react, whenever the state of a component changes, that component needs to update its UI to show the updated state. This process is known as reconciliation in react. React performs this using a virtual DOM, which is simply a representation of your actual DOM. When the state of a component changes, React compares the virtual DOM with the actual DOM and then updates the nodes of the DOM tree based on the difference between the two DOMs. This comparison is done with the help of a diffing algorithm.

The Diffing Algorithm

This is the algorithm behind React's fast updation of the virtual DOM. Similar algorithms that perform tree diff have a time complexity of O(n^3) where "n" represents the number of elements in the DOM tree. As the number of elements inside the DOM tree increases, the time complexity of these algorithms can get unbearably large. On the other hand, the diffing algorithm performs tree diffing in O(n) time, which is much better. Let us see how it works from the inside.

  1. Component Update: React re-renders the component when the prop or the component is changed. This creates a new virtual DOM.

  2. Diffing Process: The new virtual DOM is now compared to the older virtual DOM to identify the minimum number of changes required to modify the actual DOM. This comparison is done recursively, starting from the root of the tree and then going to the subsequent branches and sub-branches.

  3. Comparing Nodes: If the two nodes being compared are of the same type, React compares their attributes to identify the minimum number of changes. On the other hand, if the two nodes have different types, React assumes

  4. Comparision of children: React uses processes like component instances, list conciliation and key based conciliation to efficiently identify, move, add or remove elements.

  5. Updating the DOM: React applies the required changes based on what comes up after the diffing process. The update may include but is not limited to creating new elements, removing elements or modifying elements.

Let us understand this diffing process with an example.

const List = ({ items }) => {
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
};

Consider the above code block. When the items array changes, React not only creates a new V-DOM with the most recent updated list but also uses the key prop to identify which item has been added, removed or updated. These updates are then added to the actual DOM accordingly.

Conclusion

In conclusion, React's fast performance can be attributed to its efficient use of the virtual DOM and the highly optimized diffing algorithm. React ensures smooth and swift rendering by minimizing the number of direct interactions with the actual DOM and updating only the necessary parts of the UI. This combination of techniques allows developers to build dynamic and responsive applications without sacrificing performance. Understanding these underlying mechanisms helps developers appreciate the power and efficiency of React, making it a preferred choice for modern web development.

Did you find this article valuable?

Support Anubhav's Blog by becoming a sponsor. Any amount is appreciated!