Performance: Pure vs impure pipes
MEDIUM IMPACT
This concept affects how often Angular recalculates and updates the displayed data, impacting rendering speed and responsiveness.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'pureExample', pure: true }) export class PureExamplePipe implements PipeTransform { transform(value: any): any { // same calculation return value.map(item => item * 2); } }
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'impureExample', pure: false }) export class ImpureExamplePipe implements PipeTransform { transform(value: any): any { // expensive calculation return value.map(item => item * 2); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Pure Pipe | Minimal - only on input change | Few - only when data changes | Low - fewer repaints | [OK] Good |
| Impure Pipe | Repeated on every change detection | Many - triggers frequent reflows | High - frequent repaints | [X] Bad |