Pure pipes in Angular run only when their input values change. Angular caches the output and reuses it if the input remains the same, improving performance.
Impure pipes run on every change detection cycle regardless of input changes, which can cause performance issues if used carelessly.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'examplePipe', pure: ??? }) export class ExamplePipe implements PipeTransform { transform(value: any): any { return value; } }
Setting pure: true marks the pipe as pure, which is the default behavior. This means Angular will cache the output and only re-run the pipe when inputs change.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'slowPipe', pure: false }) export class SlowPipe implements PipeTransform { transform(value: any[]): any[] { // Simulate heavy computation for (let i = 0; i < 1000000; i++) {} return value; } }
Impure pipes run on every change detection cycle. If the pipe does heavy work, this slows down the app, especially with large lists.
Pure pipes run only when their input reference changes. By changing the list reference only when data changes, Angular avoids unnecessary pipe executions, improving performance.