Kate Ratemo

Boosting Web Performance in Angular

Boosting Web Performance in Angular

As technology continues to advance, web users don’t have the patience for slow-loading applications. Achieving top-tier performance isn’t just about writing clean code; it’s about knowing and applying the right techniques and tools to boost your app’s speed and responsiveness.

In this post, I’m sharing some of the best practices and advanced strategies I’ve learned for optimizing Angular applications. Whether you’re just starting out or already deep into development, these tips will help you create faster, more efficient apps that both you and your users will love.

Why Performance Matters

  • User Satisfaction: Fast and smooth applications keep users happy and engaged.
  • Search Engine Rankings: Faster apps are favored by search engines, improving visibility.
  • Brand Image: A performant app creates a positive impression of your brand.
  • Reduced Bounce Rates: Quick load times keep users from abandoning your app.
  • Avoid Frustration: Slow apps lead to frustration, causing users to leave.

Modern Strategies for Performance Optimization

1. Leveraging Angular Signals for Reactive Performance

Angular Signals offer a new way to handle state and reactivity in Angular applications. They allow for more precise change detection, reducing unnecessary re-renders. Using signals can significantly boost performance, particularly in complex user interfaces.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-signal-example',
  template: `
    <p>Count: 9</p>
    <button (click)="increment()">Increment</button>
  `,
})
export class SignalExampleComponent {
  count = signal(0);

  increment() {
    this.count.update((value) => value + 1);
  }
}

This example showcases how the signal feature makes state management more efficient by updating the state without causing unnecessary re-renders.

2. Standalone Components and Reduced NgModule Overhead

Standalone components, directives, and pipes help simplify your app’s structure by reducing reliance on NgModules. This results in smaller bundle sizes and faster load times. Use standalone components whenever possible to keep your applications lean and efficient.

Example:

1
2
3
4
5
6
7
8
9
import { Component } from '@angular/core';

@Component({
  standalone: true,
  selector: 'app-standalone',
  template: `<h1>Standalone Component</h1>`,
  styleUrls: ['./standalone.component.css'],
})
export class StandaloneComponent {}

This approach minimizes NgModule overhead by directly registering the component as standalone.

3. Smart Caching for Faster Data Access

One of the best ways to boost performance is by reducing unnecessary server requests. Caching lets us store data that’s frequently used, so we don’t have to keep asking the server for it every time. This helps speed things up by loading data faster and reducing the load on the server.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpResponse } from '@angular/common/http';
import { of, tap, shareReplay } from 'rxjs';

@Injectable()
export class CacheInterceptor implements HttpInterceptor {
  private cache = new Map<string, HttpResponse<any>>();

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    if (req.method !== 'GET') {
      return next.handle(req);
    }

    const cachedResponse = this.cache.get(req.urlWithParams);
    if (cachedResponse) {
      return of(cachedResponse.clone());
    }

    return next.handle(req).pipe(
      tap(event => {
        if (event instanceof HttpResponse) {
          this.cache.set(req.urlWithParams, event.clone());
        }
      }),
      shareReplay(1)
    );
  }
}

This caching mechanism ensures that GET requests are cached efficiently, reducing redundant calls to the server.

4. Optimized Image Loading with NgOptimizedImage

Leverage Angular’s NgOptimizedImage directive for better image loading performance. It automatically optimizes images, which boosts load times and improves Core Web Vitals.

Example:

1
2
<img ngOptimizedImage src="assets/image.jpg" alt="Optimized Image" width="300" height="200" />

## 5. Virtual Scrolling with @angular/cdk for Large Lists

Virtual scrolling allows you to render only the visible items in a list, which can greatly improve performance when handling large datasets.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Component } from '@angular/core';
import { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';

@Component({
  selector: 'app-virtual-scroll',
  template: `
    <cdk-virtual-scroll-viewport itemSize="50" class="viewport">
      <div *cdkVirtualFor="let item of items"></div>
    </cdk-virtual-scroll-viewport>
  `,
  styleUrls: ['./virtual-scroll.component.css'],
})
export class VirtualScrollComponent {
  items = Array.from({ length: 1000 }, (_, i) => `Item ${i + 1}`);
}

6. Lazy Loading with Dynamic Imports

Lazy loading delays the loading of feature modules until necessary. Dynamic imports are an essential part of this strategy.

7. Production Builds and Bundle Analysis

Always build your Angular apps for production with the --configuration production flag. Use tools like Webpack Bundle Analyzer to identify and reduce large bundle sizes.

Example:

1
ng build --configuration production

8. RxJS Optimization

RxJS operators like debounceTime, throttleTime, and takeUntil help efficiently manage event streams. Properly unsubscribing from observables is crucial to prevent memory leaks.

9. Server-Side Rendering (SSR) and Static Site Generation (SSG)

Implement SSR or SSG with Angular Universal to improve initial load times and SEO.

Example:

1
ng add @nguniversal/express-engine

This command sets up Angular Universal for server-side rendering, optimizing your app for SEO and load times.

10. Stay Updated with Angular Releases

Angular is continuously evolving. Keep up-to-date with new features and best practices from the official Angular documentation and community resources.