📎 Webclip
Speed Up Your Angular App - 14 Angular Optimization Tips
The article argues that updating to Angular 10 is only one step toward better performance. It divides optimization work into build and deploy changes, code-level techniques, and runtime monitoring, and it says these steps can make an app smaller, faster, and more responsive.
Reading notes#
- Angular 9 brought selector-less directives, dependency injection changes, TypeScript diagnostics improvements, and the Ivy compiler, which reduced bundle size and download times.
- Angular 10 improved template compiler performance and IDE and editor integrations through the Angular Language Service.
- A production build applies optimizations such as ahead-of-time compilation, minification, and tree-shaking.
- AOT compiles at build time, removes the Angular compiler from the deployment bundle, and reduces payload size.
- Minification removes whitespace, comments, and unreachable code, which speeds up download and execution.
- Build Optimizer removes code that is safe to drop at build time, including decorators such as @Component in AOT builds.
- Production mode makes Angular run change detection once instead of twice and can surface errors that would otherwise appear later at runtime.
- Lazy loading loads a module only when the user navigates to its route, and it depends on splitting the app into more modules.
- Code splitting reduces initial load time by breaking JavaScript into parts that load progressively, either at route level or component level.
- OnPush change detection reacts only to @Input changes or manual detection, instead of checking every component on every change.
- ngDoCheck can hold custom logic for expensive calculations or for detecting changes Angular would otherwise ignore, and caching results helps performance.
- The async pipe subscribes to observables, unsubscribes automatically, and calls markForCheck on updates, which also fits OnPush.
- Binding to functions or getters in views can slow change detection, so the article recommends avoiding them when possible.
- Pure pipes let Angular skip execution when the value has not changed.
- Zone.js patches async browser functions, while ngZone can run performance-critical code outside Angular and re-enter only when the view needs updating.
- Observables should be unsubscribed in ngOnDestroy to prevent memory leaks.
- trackBy helps ngFor update only the DOM elements that changed.
- Web workers can run CPU-heavy tasks in a background thread, but some environments do not support them and Angular CLI does not support running Angular itself in a web worker.
- Profiling tools such as Webpack Bundle Analyzer and Lighthouse help identify large bundles and performance issues.
- Angular CLI budgets let developers set size limits for bundles and warn when they grow too large.
