Tips to Optimize Your Angular App/Application
Estimated study time: 9 minutes. Concrete techniques to keep your Angular app fast as it grows.
Angular apps can slow down as they scale if performance isn't considered along the way. Here are the techniques that make the biggest difference.
1. Lazy Load Feature Modules
Instead of bundling your entire app into one file, split it by feature and load modules only when needed.
const routes: Routes = [
{
path: 'admin',
loadChildren: () =>
import('./admin/admin.module').then(m => m.AdminModule)
}
];
2. Use OnPush Change Detection
By default, Angular checks every component on every change detection cycle. Switching to OnPush tells Angular to only re-check a component when its inputs actually change.
@Component({
selector: 'app-card',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './card.component.html'
})
3. Track Items in *ngFor
Without a trackBy function, Angular re-renders an entire list when any item changes. Providing one lets it update only what actually changed.
<div *ngFor="let item of items; trackBy: trackById">{{ item.name }}</div>
4. Avoid Heavy Logic in Templates
Function calls inside templates re-run on every change detection cycle. Move expensive computations into component properties or pipes marked as pure.
5. Optimize Bundle Size
- Use
ng build --configuration productionfor tree-shaking and minification. - Analyze your bundle with
source-map-explorerto find unexpectedly large dependencies. - Prefer standalone, purpose-built libraries over large all-in-one packages.