Full answer
Templates are evaluated often. Where you put derived logic decides how many times it runs.
- Method call:
{{ fullName() }}with a normal method. Angular cannot know what the method depends on, so it calls it on every check of that view. With default change detection that can be dozens of times for one click. Cheap getters are fine, but filtering a big list or formatting many rows in a method is a common cause of slow screens. - Pure pipe:
{{ student | fullName }}. Angular callstransformagain only when the input value (reference) changes, or an argument changes. If you mutate the object in place, the pipe does not run again. Pure pipes are reusable across templates and easy to test as classes. The cache is per pipe usage in a template, not shared globally. - Computed signal:
readonly fullName = computed(() => this.first() + ' ' + this.last()). It runs when it is read and one of the signals it depends on has changed, then caches. If nothing changed, reading it again returns the cached value without running the function. It works withOnPushand zoneless apps, and dependencies are tracked automatically. - Impure pipe (
pure: false): runs on every check like a method. Use it only when the pipe depends on outside state, or on a mutated array, and the work is small. Theasyncpipe is the well-known impure pipe.
How to choose:
- Value derived from component state:
computed. - Formatting a value that comes from input, with logic reused in many places: a pure pipe.
- A tiny expression such as
a + bor a ternary: write it inline. - A method call in a template: only when it is trivial, or when it takes an event handler.
Memoizing a method by hand (storing the last argument and result) works but duplicates what pipes and computed already do.
Measure before you refactor: count calls with a counter, as in the test, or use the Angular DevTools profiler. If a method is cheap and called a few times, leave it.
typescript
import { Component, Pipe, PipeTransform, computed, signal } from '@angular/core';
export const calls = { method: 0, pipe: 0, computed: 0 };
@Pipe({ name: 'fullName' })
export class FullNamePipe implements PipeTransform {
transform(first: string): string {
calls.pipe++;
return first.toUpperCase();
}
}
@Component({
selector: 'app-name-demo',
imports: [FullNamePipe],
template: `
<p>{{ viaMethod() }}</p>
<p>{{ first | fullName }}</p>
<p>{{ viaComputed() }}</p>
`,
})
export class NameDemoComponent {
first = 'asha';
readonly firstSignal = signal('asha');
viaMethod(): string {
calls.method++;
return this.first.toUpperCase();
}
readonly viaComputed = computed(() => {
calls.computed++;
return this.firstSignal().toUpperCase();
});
}Angular 20.3, strict template check passes. A Karma test counts calls. The first render ran the method twice (development mode checks each pass twice), and after three more passes it had run 8 times, while the pure pipe and the computed each ran once. After a signal change the computed ran again.
Key points
- Template methods run on every check. Pure pipes run when the input reference changes. Computed runs when a dependency signal changes and caches.
- Use `computed` for component state and a pure pipe for reusable formatting.
- In-place mutation does not re-run a pure pipe.
- Impure pipes run every check, so keep them cheap.
Common mistakes
- Filtering or sorting a large list inside a template method.
- Mutating an array and expecting a pure pipe to update.
- Making a pipe impure to "fix" a mutation problem.
Follow-ups
- Why does the `async` pipe have to be impure?
- How would you memoize a method manually?
- What changes under zoneless change detection?
Tips
- Quote the run frequency of each option.
- Give the rule of choice in two lines.
- Mention measuring before refactoring.
Evaluation rubric
- States how often each option runs.
- Understands caching and dependency tracking.
- Makes a reasoned choice.