Calling functions in Angular templates looks clean:
<p>{{ getFullName() }}</p>
But this pattern has a hidden cost.
It’s not about correctness — it’s about how often Angular executes that function.
⚙️ What’s Really Happening
Angular does not cache template function calls.
Every change detection cycle re-evaluates:
{{ getFullName() }}
Even if:
- the data hasn’t changed
- the result is identical
- nothing visible updates
The function still runs.
🚨 Why This Becomes a Problem
- Change Detection Runs More Than You Think
Triggered by:
- user interactions
- async events
- signals updating
- parent component changes
Each trigger = function execution again.
- It Explodes Inside Lists
<li *ngFor="let user of users">
{{ getFullName(user) }}
</li>
100 items = 100 executions
Per cycle.
Now add:
- filtering
- formatting
- derived calculations
That’s where performance quietly degrades.
- Cost Is Invisible Until It’s Not
The real issue isn’t small functions — it’s what they turn into over time.
Today:
return user.name;
Tomorrow:
- formatting
- conditionals
- nested logic
Same template. Very different cost.
❌ The Problem Pattern
getFullName(user: User) {
return `${user.firstName} ${user.lastName}`;
}
<p>{{ getFullName(user) }}</p>
Looks harmless. Scales poorly.
✅ Better Patterns
- Precompute Values
user = {
...rawUser,
fullName: `${rawUser.firstName} ${rawUser.lastName}`
};
<p>{{ user.fullName }}</p>
Simple. Predictable. Fast.
- Use Signals (computed)
fullName = computed(() =>
`${this.user().firstName} ${this.user().lastName}`
);
<p>{{ fullName() }}</p>
Recomputes only when dependencies change.
- Use Pure Pipes
<p>{{ user | fullName }}</p>
Pure pipes are memoized by Angular — they don’t recompute unnecessarily.
🧠 When Is It Actually Okay?
Template function calls are acceptable if:
- the logic is trivial
- not inside loops
- not called frequently
But in most real apps:
Template functions are a scaling risk, not an immediate bug.
⚡ Rule of Thumb
If it’s inside a template:
- assume it runs many times
- keep it cheap or precomputed
🔥 Final Thought
Angular performance isn’t about micro-optimizations.
It’s about predictability.
Templates should describe UI —
not execute logic repeatedly.
What feels clean now can become your slowest path later.
Top comments (0)