0
0
Angularframework~10 mins

Pure vs impure pipes in Angular - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Pure vs impure pipes
Input value changes?
Yes
Pure Pipe transforms input
Output updated in template
Wait for next change
No input change?
No
Pure Pipe skips transform
Impure Pipe triggers
Transforms input every change detection
Output updated in template
Wait for next change detection
Pure pipes run only when input changes; impure pipes run every change detection cycle.
Execution Sample
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'example', pure: true })
export class ExamplePipe implements PipeTransform {
  transform(value: any): any {
    return value + '!';
  }
}
A simple pure pipe that adds an exclamation mark to the input string.
Execution Table
StepChange Detection CycleInput ValuePipe TypeTransform Called?OutputReason
1Initial'Hello'PureYes'Hello!'Input changed from undefined to 'Hello'
2Second'Hello'PureNo'Hello!'Input unchanged, pure pipe skips transform
3Third'Hello World'PureYes'Hello World!'Input changed, pure pipe transforms
4Fourth'Hello World'PureNo'Hello World!'Input unchanged, pure pipe skips transform
5Initial'Hello'ImpureYes'Hello!'Impure pipe always transforms
6Second'Hello'ImpureYes'Hello!'Impure pipe always transforms
7Third'Hello World'ImpureYes'Hello World!'Impure pipe always transforms
8Fourth'Hello World'ImpureYes'Hello World!'Impure pipe always transforms
💡 Pure pipe stops transforming when input unchanged; impure pipe transforms every cycle.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8
inputValueundefined'Hello''Hello''Hello World''Hello World''Hello''Hello''Hello World''Hello World'
purePipeOutputundefined'Hello!''Hello!''Hello World!''Hello World!'N/AN/AN/AN/A
impurePipeOutputundefinedN/AN/AN/AN/A'Hello!''Hello!''Hello World!''Hello World!'
Key Moments - 3 Insights
Why does the pure pipe not transform the input on every change detection?
Because pure pipes only run when their input changes, as shown in execution_table rows 2 and 4 where input is unchanged and transform is skipped.
Why does the impure pipe transform the input every time even if the input is the same?
Impure pipes run on every change detection cycle regardless of input change, as seen in execution_table rows 5 to 8 where transform is always called.
What happens if the input is an object that changes internally but reference stays the same for a pure pipe?
Pure pipe will not detect internal changes because it checks input by reference, so transform won't run unless the object reference changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the pure pipe first skip transforming the input?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Check the 'Transform Called?' column for pure pipe rows in execution_table.
According to variable_tracker, what is the purePipeOutput after the third change detection?
A'Hello!'
B'Hello World!'
C'Hello World'
Dundefined
💡 Hint
Look at the purePipeOutput row under 'After 3' column in variable_tracker.
If the input value changes every cycle, how would the pure pipe's transform calls change?
ATransform called every cycle
BTransform called only once
CTransform never called
DTransform called randomly
💡 Hint
Pure pipes run transform when input changes, see execution_table steps 1 and 3.
Concept Snapshot
Pure pipes run only when input reference changes.
Impure pipes run every change detection cycle.
Use pure pipes for performance when input is immutable.
Impure pipes useful for inputs that change internally.
Declare pipe with @Pipe({pure: true|false}).
Angular calls transform accordingly.
Full Transcript
In Angular, pipes transform data in templates. Pure pipes run their transform method only when the input changes by reference, improving performance by skipping unnecessary work. Impure pipes run their transform method every change detection cycle, regardless of input changes, useful when input changes internally without reference change. The execution table shows pure pipes skipping transform when input is unchanged, while impure pipes always transform. Variable tracker shows how outputs update accordingly. Key moments clarify why pure pipes skip transforms and impure pipes do not. The visual quiz tests understanding of when transforms run and output values. Remember to choose pure pipes for stable inputs and impure pipes for dynamic inputs.