Performance: Creating custom pipes
MEDIUM IMPACT
Custom pipes affect rendering speed by how often they run during change detection and how complex their logic is.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'goodPipe', pure: true }) export class GoodPipe implements PipeTransform { transform(value: any): any { // simple or memoized logic return fastCalculation(value); } } // Usage in template: {{ data | goodPipe }}
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'badPipe', pure: false }) export class BadPipe implements PipeTransform { transform(value: any): any { // complex logic return heavyCalculation(value); } } // Usage in template: {{ data | badPipe }}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Impure pipe with heavy logic | No extra DOM nodes | Multiple recalculations cause layout thrashing if UI updates | High CPU paint blocking | [X] Bad |
| Pure pipe with simple logic | No extra DOM nodes | Minimal recalculations only on input change | Low CPU paint blocking | [OK] Good |