DEV Community

Cover image for Stop Calling Functions in Angular Templates — It’s Slower Than You Think
Mridu Dixit
Mridu Dixit

Posted on

Stop Calling Functions in Angular Templates — It’s Slower Than You Think

Calling functions in Angular templates looks clean:

<p>{{ getFullName() }}</p>
Enter fullscreen mode Exit fullscreen mode

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() }}
Enter fullscreen mode Exit fullscreen mode

Even if:

  • the data hasn’t changed
  • the result is identical
  • nothing visible updates

The function still runs.

🚨 Why This Becomes a Problem

  1. Change Detection Runs More Than You Think

Triggered by:

  • user interactions
  • async events
  • signals updating
  • parent component changes

Each trigger = function execution again.

  1. It Explodes Inside Lists
<li *ngFor="let user of users">
  {{ getFullName(user) }}
</li>
Enter fullscreen mode Exit fullscreen mode

100 items = 100 executions
Per cycle.

Now add:

  • filtering
  • formatting
  • derived calculations

That’s where performance quietly degrades.

  1. 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;
Enter fullscreen mode Exit fullscreen mode

Tomorrow:

  • formatting
  • conditionals
  • nested logic

Same template. Very different cost.

❌ The Problem Pattern

getFullName(user: User) {
  return `${user.firstName} ${user.lastName}`;
}
Enter fullscreen mode Exit fullscreen mode
<p>{{ getFullName(user) }}</p>
Enter fullscreen mode Exit fullscreen mode

Looks harmless. Scales poorly.

✅ Better Patterns

  1. Precompute Values
user = {
  ...rawUser,
  fullName: `${rawUser.firstName} ${rawUser.lastName}`
};
Enter fullscreen mode Exit fullscreen mode
<p>{{ user.fullName }}</p>
Enter fullscreen mode Exit fullscreen mode

Simple. Predictable. Fast.

  1. Use Signals (computed)
fullName = computed(() =>
  `${this.user().firstName} ${this.user().lastName}`
);
Enter fullscreen mode Exit fullscreen mode
<p>{{ fullName() }}</p>
Enter fullscreen mode Exit fullscreen mode

Recomputes only when dependencies change.

  1. Use Pure Pipes
<p>{{ user | fullName }}</p>
Enter fullscreen mode Exit fullscreen mode

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)