Back to results for “Performance & Lazy Loading”
Angular › Pipes (Built-in & Custom)

What is the difference between a pure and an impure pipe in Angular, and when would you need an impure one?

Advanced

Answer

A pure pipe, the default type, only recalculates its output when its actual input value changes by reference, making it very efficient. An impure pipe recalculates on every single change detection cycle regardless of whether the input reference changed, which is sometimes necessary for pipes that need to react to changes inside an array or object without a new reference being created, though it can hurt performance if used carelessly.
@Pipe({ name: 'filterActive', pure: false })
export class FilterActivePipe implements PipeTransform {
  transform(items: Item[]): Item[] {
    return items.filter((item) => item.active);
  }
}
// Recalculates every cycle, since the array reference itself might not change even when its contents do
Real-world example A team debugging why a filtering pipe was not updating after items were mutated directly inside an array discovers the pipe needed to be marked impure, since the array reference itself never actually changed.

Common follow-ups: Why can overusing impure pipes hurt performance in a large application?;What is a better long term fix than marking a pipe impure, related to how the underlying data is updated?