CodexBloom - Programming Q&A Platform

Angular 16 - implementing performance optimization When Rendering Large Lists with ngFor and TrackBy Function

πŸ‘€ Views: 46 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-05
angular performance ngFor typescript

I'm performance testing and Could someone explain I've searched everywhere and can't find a clear answer. I'm experiencing important performance optimization in my Angular 16 application when rendering a large list of items using `*ngFor`. The list can contain over 10,000 items, and the application becomes sluggish and unresponsive when this list is displayed on the UI. I've implemented the `trackBy` function to optimize the rendering, but it doesn't seem to be making a noticeable difference. Here’s a simplified version of my component code: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-item-list', template: ` <ul> <li *ngFor="let item of items; trackBy: trackById">{{ item.name }}</li> </ul> `, styleUrls: ['./item-list.component.css'] }) export class ItemListComponent { items = Array.from({ length: 10000 }, (_, i) => ({ id: i, name: `Item ${i}` })); trackById(index: number, item: any): number { return item.id; } } ``` While I thought using `trackBy` would help Angular identify which items have changed and optimize the rendering, I've noticed that rendering times are still quite high and the browser becomes unresponsive during the initial load. I've also tried implementing `ChangeDetectionStrategy.OnPush`, but that hasn't improved performance either. For context, I'm using Chrome, and I've checked the performance using the DevTools, which indicates excessive reflows and repaints during rendering. Is there any additional technique or best practice I might be missing that could help improve performance when dealing with large lists in Angular? Any suggestions would be greatly appreciated! Am I missing something obvious? The project is a mobile app built with Typescript. Any help would be greatly appreciated! Thanks for taking the time to read this!