Performance: Parameterized pipes
MEDIUM IMPACT
This affects rendering speed and responsiveness by how often Angular recalculates and updates the pipe output when parameters change.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'expensiveFormat', pure: true }) export class ExpensiveFormatPipe implements PipeTransform { private cachedValue = ''; private cachedParam = 0; private cachedResult = ''; transform(value: string, param: number): string { if (value !== this.cachedValue || param !== this.cachedParam) { // Expensive calculation only when inputs change for (let i = 0; i < 1000000; i++) {} this.cachedResult = value.repeat(param); this.cachedValue = value; this.cachedParam = param; } return this.cachedResult; } } // Usage in template: // {{ someText | expensiveFormat: dynamicParam }}
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'expensiveFormat', pure: true }) export class ExpensiveFormatPipe implements PipeTransform { transform(value: string, param: number): string { // Simulate expensive calculation for (let i = 0; i < 1000000; i++) {} return value.repeat(param); } } // Usage in template: // {{ someText | expensiveFormat: dynamicParam }}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Expensive parameterized pipe without caching | Minimal | 0 | High due to blocking JS | [X] Bad |
| Cached parameterized pipe with pure/impure flag | Minimal | 0 | Low, non-blocking | [OK] Good |