Sub topic 11
Pure versus impure pipes
This chapter assumes you can write a custom pipe with @Pipe and PipeTransform, and that you know how standalone components import pipes. Here you will learn when Angular calls a pipe's transform method and what that means for your code.
Pure pipes are the default
A pipe is pure unless you say otherwise. Angular calls a pure pipe's transform method only when one of its inputs changes. It checks this with a cheap comparison:
- For a primitive such as a string, number or boolean, a pure pipe re-runs when the value is different.
- For an object or array, a pure pipe re-runs only when the reference is different. Changes inside the same object are not noticed.
If nothing changed, Angular reuses the previous result. This makes pure pipes fast, and all the built-in formatting pipes (date, currency, uppercase and so on) are pure.
The common error: mutating an array
The filter pipe below is pure, because it leaves out the pure option.
import { Pipe, PipeTransform } from '@angular/core';
export interface Task {
name: string;
active: boolean;
}
@Pipe({ name: 'activeOnly', standalone: true })
export class ActiveOnlyPipe implements PipeTransform {
transform(items: Task[]): Task[] {
return items.filter((item) => item.active);
}
}Now use it in a component that adds tasks to the array.
import { Component } from '@angular/core';
import { ActiveOnlyPipe, Task } from './active-only.pipe';
@Component({
selector: 'app-task-list',
standalone: true,
imports: [ActiveOnlyPipe],
template: `
<button (click)="addByMutation()">Push</button>
<button (click)="addByReplacement()">Replace</button>
<ul>
@for (task of tasks | activeOnly; track task.name) {
<li>{{ task.name }}</li>
}
</ul>
`,
})
export class TaskListComponent {
tasks: Task[] = [{ name: 'Write chapter', active: true }];
addByMutation(): void {
this.tasks.push({ name: 'Pushed task', active: true });
}
addByReplacement(): void {
this.tasks = [...this.tasks, { name: 'Replaced task', active: true }];
}
}addByMutation changes the array in place. The reference stays the same, so the pure pipe does not run and the list does not update. addByReplacement creates a new array. The reference changes, the pipe runs, and the list updates. Note that the replacement also shows any earlier pushed tasks, because the new array is copied from the mutated one.
Impure pipes run on every cycle
Setting pure: false tells Angular to call transform on every change detection cycle, whatever changed.
import { Pipe, PipeTransform } from '@angular/core';
import { Task } from './active-only.pipe';
@Pipe({ name: 'activeOnlyLive', standalone: true, pure: false })
export class ActiveOnlyLivePipe implements PipeTransform {
transform(items: Task[]): Task[] {
return items.filter((item) => item.active);
}
}With this version, addByMutation also updates the list. The cost is that the filter runs after every click, keystroke, timer and HTTP response anywhere in the app's change detection pass, even when the array is untouched.
The built-in async pipe is impure for the same reason. It must check on each cycle whether its Observable has emitted a new value.
Choosing between them
| Situation | Choice |
|---|---|
| Input is a primitive or you replace objects and arrays immutably | Pure pipe |
| The work is expensive | Pure pipe |
| Data is mutated in place and you cannot change that | Impure pipe, kept cheap |
| The pipe depends on state outside its inputs | Impure pipe, or avoid the pipe |
Prefer a pure pipe and update data immutably. If you must use an impure pipe, keep transform fast and free of side effects. Because it runs so often, it may also return a new array each time, which can cause extra re-rendering of lists.
Exercise
A pure pipe sortByName receives people from a component. A button calls this.people.push(newPerson), but the sorted list on screen does not change. Give two ways to fix this.
Show answer
The pure pipe only runs when the people reference changes, and push keeps the same reference. Fix 1: replace the array instead of mutating it, for example this.people = [...this.people, newPerson]. Fix 2: mark the pipe with pure: false so it runs on every change detection cycle, accepting the extra cost. The first fix is usually better.