0
0
Angularframework~8 mins

Parameterized pipes in Angular - Performance & Optimization

Choose your learning style9 modes available
Performance: Parameterized pipes
MEDIUM IMPACT
This affects rendering speed and responsiveness by how often Angular recalculates and updates the pipe output when parameters change.
Using a parameterized pipe to format data with changing parameters
Angular
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 }}
Caches results and recalculates only when inputs change, reducing unnecessary work during change detection.
📈 Performance GainReduces blocking time to near zero on unchanged inputs, improving input responsiveness.
Using a parameterized pipe to format data with changing parameters
Angular
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 }}
The pipe runs on every change detection because the parameter changes frequently, causing expensive recalculations.
📉 Performance CostBlocks rendering for 10-50ms per change detection, causing input lag and slow UI updates.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Expensive parameterized pipe without cachingMinimal0High due to blocking JS[X] Bad
Cached parameterized pipe with pure/impure flagMinimal0Low, non-blocking[OK] Good
Rendering Pipeline
Angular runs pipes during change detection. Parameterized pipes with changing inputs trigger recalculation, affecting the rendering pipeline stages.
Change Detection
Style Calculation
Layout
Paint
⚠️ BottleneckChange Detection recalculations caused by expensive pipe transforms
Core Web Vital Affected
INP
This affects rendering speed and responsiveness by how often Angular recalculates and updates the pipe output when parameters change.
Optimization Tips
1Avoid expensive calculations inside parameterized pipes that run on every change detection.
2Cache pipe results and recalculate only when input parameters change.
3Use pure pipes when possible to minimize recalculations.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of using parameterized pipes in Angular?
AThey block network requests during rendering.
BThey can cause expensive recalculations on every change detection if parameters change frequently.
CThey increase the number of DOM nodes created.
DThey cause CSS recalculations on every frame.
DevTools: Performance
How to check: Record a performance profile while interacting with the UI that triggers the pipe. Look for long scripting tasks during change detection.
What to look for: Look for long 'Update View' or 'Change Detection' tasks that block the main thread and cause input lag.