Skip to content

React vs Angular: Who is better in rendering?

React vs Angular: Who is better in rendering?

In this article we will try to understand the difference between how React vs Angular rendering. Both have different approaches to rendering and updating the user interface, which can have an impact on their performance and ease of use.

React Rendering

React uses a virtual DOM to manage the UI updates. When a component’s state changes, React creates a new virtual DOM tree that represents the updated UI, and then compares it to the previous virtual DOM tree to determine which parts of the actual DOM need to be updated. React then efficiently updates only those parts of the DOM that have changed, resulting in a more performant UI.

Here’s an example of how this works in practice. Suppose we have a simple component that displays a counter value and a button to increment it:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const handleClick = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

When the user clicks the “Increment” button, React will create a new virtual DOM tree with the updated count value and compare it to the previous virtual DOM tree. React will then update only the parts of the actual DOM that have changed (in this case, the count value).

Angular Rendering

Angular , on the other hand, uses a different approach called “Zone.js” to handle rendering and updates. Zone.js is a library that provides a mechanism for tracking changes in the application state and automatically updating the UI in response to those changes. When a change occurs, Zone.js triggers a “change detection” cycle that updates the UI accordingly.

Here’s an example of how this works in practice. Suppose we have a simple component that displays a list of items and a button to add a new item:

import { Component } from '@angular/core';

@Component({
  selector: 'app-item-list',
  template: `
    <div>
      <ul>
        <li *ngFor="let item of items">{{item}}</li>
      </ul>
      <button (click)="addItem()">Add Item</button>
    </div>
  `,
})
export class ItemListComponent {
  items = ['item 1', 'item 2', 'item 3'];

  addItem() {
    this.items.push(`item ${this.items.length + 1}`);
  }
}

When the user clicks the “Add Item” button, Angular will update the items array and trigger a change detection cycle to update the UI. In this case, the new item will be added to the list in the view.

Conclusion

In both React and Angular, the UI updates are efficient and optimized to minimize unnecessary updates to the DOM. However, the approaches used by the two frameworks are different, with React using a virtual DOM and Angular using change detection.

One of the key differences between React and Angular is that React is focused on creating reusable UI components, while Angular is more focused on building large-scale applications. React allows developers to easily create and reuse UI components across different parts of an application, while Angular provides a more comprehensive framework for building complex web applications.

In terms of performance, React is generally considered to be faster than Angular due to its efficient virtual DOM-based rendering approach. However, Angular has a powerful set of built-in features and a more comprehensive framework that can make it easier to build large and complex applications.

Ultimately, the choice between React and Angular will depend on the specific needs and requirements of your project.

Further Reading

React

Angular

How to use React Memo: Actually improve page performance

Please share