Angular & Signals (283 Qs)•Part 1: Architecture, Components, Directives & Lifecycle (1 - 35)
What is the difference between pure and impure pipe?
A pure pipe is only called when Angular detects a change in the value or the parameters passed to a pipe. For example, any changes to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object). An impure pipe is called for every change detection cycle no matter whether the value or parameters changes. i.e, An impure pipe is called often, as often as every keystroke or mouse-move.
Practical Code Example
typescript
// Pure Pipe: Re-evaluates ONLY when input primitive value or object reference changes
@Pipe({ name: 'pureFilter', pure: true, standalone: true })
export class PureFilterPipe implements PipeTransform {
transform(items: string[], query: string): string[] {
return items.filter(i => i.includes(query));
}
}
// Impure Pipe: Re-evaluates on EVERY change detection cycle (expensive!)
@Pipe({ name: 'impureFilter', pure: false, standalone: true })
export class ImpureFilterPipe implements PipeTransform {
transform(items: any[]): any[] { return items; }
}