Performance: Pipe performance considerations
MEDIUM IMPACT
This affects the rendering speed and responsiveness by controlling how often Angular recalculates and updates displayed data using pipes.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'purePipe', pure: true }) export class PurePipe implements PipeTransform { transform(value: any): any { // simple or cached calculation return value.toUpperCase(); } }
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'expensivePipe', pure: false }) export class ExpensivePipe implements PipeTransform { transform(value: any): any { // heavy calculation or data fetch let result = 0; for(let i = 0; i < 1000000; i++) { result += i; } return result + value; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Impure Pipe with heavy logic | Minimal DOM changes but frequent recalculations | Triggers multiple reflows indirectly due to slow updates | High paint cost due to blocking UI thread | [X] Bad |
| Pure Pipe with simple logic | Minimal DOM changes and recalculations only on input change | Single reflow per input change | Low paint cost, smooth UI | [OK] Good |