How can I diagnose performance issues in an Angular application using measurements?
0 reputation · 11 Sept 2023, 18:04 UTC
0 reputation · 11 Sept 2023, 18:04 UTC
What steps and tools are recommended for measuring and diagnosing performance bottlenecks in an Angular app, such as change detection, rendering time, and bundle size?
34775 reputation · 11 Sept 2023, 20:12 UTC
Diagnosing performance bottlenecks in an Angular application relies on collecting quantitative data before and after changes. The following procedure assumes you have access to Chrome DevTools, the Angular DevTools extension, webpack‑bundle‑analyzer (or source‑map‑explorer), and Lighthouse. Adjust tool versions to match your project’s Node and Angular CLI versions.
ng build --configuration production) and serve it locally. Record:webpack-bundle-analyzer or source-map-explorer).OnPush change detection or memoizing inputs.OnPush to a component, split a lazy route, or replace a large library).Throughout the process, keep a simple spreadsheet or markdown log of each experiment, the tools used, the raw numbers, and the conclusion. This log aids reproducibility and helps communicate findings to the team.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 12 Sept 2023, 02:38 UTC
To refine the diagnosis of rendering bottlenecks, it is useful to distinguish between JavaScript execution time and browser painting overhead. While the Angular DevTools Profiler identifies which components are re-rendering, the Chrome DevTools Performance tab reveals why the browser is struggling.
zone.js entries. If you see frequent, short tasks triggering change detection for minor events (like mouse moves), verify if those events can be moved outside the Angular zone using ngZone.runOutsideAngular().offsetHeight) immediately after a style change, forcing the browser to recalculate the layout prematurely.For precise timing of custom logic, consider using the window.performance.mark() and measure() APIs, which integrate directly into the DevTools "Timings" row for a more granular view than the general flame graph.