General Performance Guidelines for Angular

In modern web development, performance is crucial for delivering responsive and efficient applications. Angular, as a powerful framework, offers various features and optimizations to enhance performance. This blog post outlines best practices to ensure your Angular applications run smoothly and efficiently.

ChangeDetectionStrategy.onPush Strategy

For better performance, it’s recommended to use ChangeDetectionStrategy.onPush for all components unless a specific component requires the default strategy.

The default strategy forces Angular to rerun change detection on all components that exist on the page, even if a user interaction only changes one variable in a small component. Usually, Angular runs change detection on every user interaction with the page (click, scroll, focus, input) and it might not be an issue for small applications but becomes a problem when an application becomes larger. As the application grows, each change detection cycle takes more time, leading to sluggishness and potential freezes.

When using ChangeDetectionStrategy.onPush, Angular is instructed to only run change detection on that component in specific scenarios, but only run this check and do component rerender on a restricted, smaller amount of rules:

  • When component Input() properties change
  • When a new value is propagated into a stream which is used in a template with an | async pipe
  • When a user interacts with elements that are defined inside this component
  • When child components emit an event and this parent component is subscribed to it
  • If you manually trigger change detection using ChangeDetectorRef.detectChanges() or ChangeDetectorRef.markForCheck().

Although it restricts the developer about how the code should be written in the app, in the long term it should significantly improve application performance and speed up application response to user actions.

Avoid Heavy Functions in Templates (Except Event Handlers)

During each rerender cycle angular checks the template properties, and rerenders the view if those properties have changed. Using functions (especially heavy ones) for assigning some property in template would force angular to run that function in EACH rerender cycle (that might happen even 5-10 times per second). That makes any user interaction with an app really slow.

Suggestion: Cache the function result into a component property and use that instead

Bad example: 

<div [class.some-class]=”someHeavyFunction()”>{{anotherHeavyFunction()}}</div>

Good example:

<div [class.some-class]=”someProp”>{{anotherProp}}</div>

Lazy Load Pages or Modules

The main thing that affects the initial app load time in Angular is the core bundle size (main.js). The best way to significantly improve the loading time – to split up the application on the independent lazy loaded modules for each page. 

Lazy loading some heavy pages or libraries would significantly reduce the main bundle size and improve the first app load speed 

Data Lazy Loading / Lazy Rendering

Rendering a large amount of data on the page might cause a freeze that is visible to a user. Using lazy loading/rendering for large data sets (lists, tables, etc…) that should be displayed on the page should resolve that issue. In general, the rule is: render only a small part of data that would fit into one-two screens and then lazy load or lazy render the rest part of data while a user scrolls or paginates to the next page.

So there are few rules and techniques that we should consider during development:

Browser Events Handling

Usually browser events (scroll for example) generate a lot of events during one second (10-100). For development purposes we usually don’t need all of them, so it might be useful to limit them by using following rxjs operators: debounceTime, auditTime, throttleTime.

Note: Handling of any browser event in the app would also trigger angular change detection cycle that might cause performance issues described in `ChangeDetectionStrategy.onPush strategy` section above. So limiting them would have a positive effect on the application performance in general.

Use `pure` Flag for Pipes

This flag indicates that the pipe is pure, which means that Angular executes the pipe only when it detects a pure change to the input value. It allows angular to cache results and to skip redundant calculations on each change detection run, if input params for a pipe were not changed.

Conclusion

Prioritizing performance in Angular applications is essential to delivering a superior user experience. By following best practices such as optimizing change detection, leveraging lazy loading, and managing event handlers and subscriptions efficiently, developers can build responsive and efficient applications. Regular performance monitoring and staying up to date with Angular’s latest features further enhance performance.

Implementing these guidelines will help ensure that your Angular applications not only meet user expectations but also stand out in a competitive landscape.

If you’re experiencing performance issues in your Angular applications or have custom software needs, don’t hesitate to contact Trailhead. Our expert team is here to help you optimize your applications and achieve your business goals.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.