Pipes help change data in templates. Pure and impure pipes decide when to update the displayed data.
Pure vs impure pipes in Angular
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'examplePipe', pure: true // or false }) export class ExamplePipe implements PipeTransform { transform(value: any, ...args: any[]): any { // transform logic here return value; } }
The pure property controls if the pipe is pure (true) or impure (false).
Pure pipes run only when input references change. Impure pipes run on every change detection cycle.
@Pipe({ name: 'purePipe', pure: true })
export class PurePipe implements PipeTransform {
transform(value: string): string {
return value.toUpperCase();
}
}@Pipe({ name: 'impurePipe', pure: false })
export class ImpurePipe implements PipeTransform {
transform(value: string): string {
return value + ' updated';
}
}This example shows two pipes: one pure and one impure. Clicking the button sets the text to the same string. The pure pipe runs only if the input reference changes, so it may not run. The impure pipe runs every time Angular checks for changes, so it always runs.
import { Component, Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'purePipe', pure: true }) export class PurePipe implements PipeTransform { transform(value: string): string { console.log('PurePipe transform called'); return value.toUpperCase(); } } @Pipe({ name: 'impurePipe', pure: false }) export class ImpurePipe implements PipeTransform { transform(value: string): string { console.log('ImpurePipe transform called'); return value + ' updated'; } } @Component({ selector: 'app-root', template: ` <h3>Pure Pipe Output: {{ text | purePipe }}</h3> <h3>Impure Pipe Output: {{ text | impurePipe }}</h3> <button (click)="changeText()">Change Text</button> ` }) export class AppComponent { text = 'hello'; changeText() { this.text = 'hello'; // same string value, new reference } }
Pure pipes improve performance by running less often.
Impure pipes can slow down your app if overused because they run very often.
Use impure pipes only when you need to detect changes inside objects or arrays without changing their reference.
Pure pipes run only when input references change.
Impure pipes run on every change detection cycle.
Choose pure pipes for better performance and impure pipes when you need frequent updates.
Practice
Solution
Step 1: Understand pure pipe behavior
Pure pipes execute only when the input reference changes, meaning they do not run on every change detection cycle.Step 2: Understand impure pipe behavior
Impure pipes run on every change detection cycle regardless of input changes, which can impact performance.Final Answer:
Pure pipes run only when input references change, impure pipes run every change detection cycle. -> Option CQuick Check:
Pure vs impure pipe execution = A [OK]
- Confusing when pure and impure pipes run
- Thinking pure pipes run every cycle
- Assuming impure pipes only run on input change
Solution
Step 1: Recall pipe metadata syntax
Angular pipes use the @Pipe decorator with a 'pure' property to indicate purity.Step 2: Identify impure pipe declaration
Setting 'pure: false' marks the pipe as impure, causing it to run every change detection cycle.Final Answer:
@Pipe({ name: 'examplePipe', pure: false }) -> Option AQuick Check:
Impure pipe uses pure: false [OK]
- Using pure: true for impure pipes
- Omitting pure property defaults to pure
- Using non-existent 'impure' property
items = [1, 2, 3];
// Template: {{ items | customPipe }}If
customPipe is pure and the array items is mutated by items.push(4) without changing the reference, what will happen?Solution
Step 1: Understand pure pipe input change detection
Pure pipes run only when the input reference changes, not when the content inside the object changes.Step 2: Analyze mutation effect on pipe execution
Mutating the array with push() does not change the reference, so the pure pipe does not run again and output stays the same.Final Answer:
The pipe will not run; output remains unchanged. -> Option DQuick Check:
Pure pipe ignores internal mutations = C [OK]
- Assuming mutation triggers pure pipe
- Thinking Angular throws error on mutation
- Believing manual change detection is required here
@Pipe({ name: 'filterPipe', pure: true }). It filters an array based on user input. The filtered results do not update when the user types. What is the likely cause?Solution
Step 1: Identify pipe purity and input behavior
A pure pipe runs only when the input reference changes, not when the content changes.Step 2: Understand why filtered results don't update
If the array is filtered in place without creating a new array reference, the pure pipe does not detect change and does not update output.Final Answer:
The pipe is pure and input array reference does not change on filtering. -> Option BQuick Check:
Pure pipe needs new input reference to update [OK]
- Assuming impure pipe causes this issue
- Forgetting to return new array reference
- Ignoring pipe naming or method errors
Solution
Step 1: Understand the need for frequent updates
Formatting current time every second requires the pipe to run frequently to update the display.Step 2: Choose pipe type based on update frequency
Impure pipes run every change detection cycle, making them suitable for frequent updates like a clock.Final Answer:
Impure pipe, because it runs every change detection cycle and updates time frequently. -> Option AQuick Check:
Frequent updates need impure pipe [OK]
- Choosing pure pipe for frequent updates
- Thinking pure pipes auto-update every second
- Confusing pipe purpose with formatting capability
Ready to prove it?
Pass the test and this pattern moves unknown
