How to improve Angular performance

Mushegh Zakaryan
3 min readFeb 19, 2023

--

Angular is a popular front-end framework for building web applications. However, as your application grows in size and complexity, you may notice a decrease in performance. In this article, we will discuss some of the best practices for improving the performance of your Angular application.

Use OnPush change detection strategy

Angular uses change detection to determine when to update the view. The default change detection strategy checks for changes in all components every time an event occurs. This can be a time-consuming process, especially for large applications.

To improve performance, you can use the OnPush change detection strategy. The “OnPush” strategy will trigger change detection only if the reference to an input property has changed, not if the object’s properties themselves have changed. This means that if you mutate an object that is passed as an input property, change detection will not be triggered, but if you assign a new object to the input property, change detection will be triggered.

This behavior is different from the default change detection strategy, which triggers change detection on all input property changes regardless of whether the reference to the input property has changed or not.

To use the OnPush change detection strategy, add the following code to your component:

@Component({
changeDetection: ChangeDetectionStrategy.OnPush
})

Use lazy loading

Lazy loading is a technique that allows you to load only the required components and modules on demand. This can reduce the initial load time of your application and improve its performance.

To use lazy loading, you can split your application into feature modules and load them only when they are needed. For example, if your application has a user profile page, you can create a separate module for it and load it only when the user navigates to that page.

Optimize component initialization

Component initialization can be a time-consuming process, especially if you have a large number of dependencies or if you perform unnecessary computations during initialization.

To optimize component initialization, you can minimize the number of dependencies and avoid unnecessary computations during initialization. You can also use lazy loading to load dependencies only when they are needed.

Use trackBy in ngFor

If you are using ngFor to loop through a list of items, use trackBy to help Angular track which items have been added, removed, or modified. This can improve the performance of your application by reducing the number of DOM manipulations.

To use trackBy, add the following code to your component:

<ng-container *ngFor="let item of items; trackBy: trackByFn">
{{ item }}
</ng-container>

...

trackByFn(index, item) {
return item.id;
}

In this example, the trackByFn function returns the ID of the item. This helps Angular track which items have been added, removed, or modified.

Use pure pipes

Pipes are used to transform data in Angular. By default, pipes are pure, but you can have impure pipes which means that they are called every time there is a change in the data. This can be a performance issue if the pipe does not need to be recalculated frequently.

To improve performance, you can make your pipe pure. A pure pipe is optimized to only recalculate when the input value changes. To make your pipe pure, add the following code to your pipe:

@Pipe({
name: 'myPipe',
pure: true
})

In conclusion, improving the performance of your Angular application can be achieved through various techniques, including using the OnPush change detection strategy, lazy loading, AOT compilation, optimizing component initialization, using trackBy in ngFor, using pure pipes, and enabling production mode.

By following these best practices, you can significantly improve the performance of your Angular application and deliver a better user experience for your users.

Read also: https://zmushegh.medium.com/why-use-pipe-instead-of-function-in-angular-507cf972bfb0

Best Regards,
Mushegh Zakaryan

--

--

Mushegh Zakaryan
Mushegh Zakaryan

Written by Mushegh Zakaryan

Senior Front End Engineer / Angular

No responses yet