Why is calling a method in a template expression a performance problem?
Model answer
Templates are checked often. A method call such as {{ getTotal() }} runs every time change detection checks that binding, even when nothing relevant changed, so an expensive method runs again and again.
Better options: compute the value once into a signal or computed, use a pure pipe, or store the result in a variable when the data changes. Simple getters of a signal, such as total(), are fine because signals cache computed results.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- Why is track important in @for (or trackBy in *ngFor)?
- What are the main ways to improve Angular app performance?
- What is NgOptimizedImage and what does it give you?
- What is server-side rendering (SSR), and why would you use it?
- What is hydration in Angular?
- What is the difference between SSR, SSG (prerendering) and CSR, and how do you choose?