Bird
Raised Fist0
Angularframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between a pure pipe and an impure pipe in Angular?
easy
A. Pure pipes can modify input data, impure pipes cannot.
B. Pure pipes run every change detection cycle, impure pipes run only when input references change.
C. Pure pipes run only when input references change, impure pipes run every change detection cycle.
D. Pure pipes are slower than impure pipes.

Solution

  1. 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.
  2. Step 2: Understand impure pipe behavior

    Impure pipes run on every change detection cycle regardless of input changes, which can impact performance.
  3. Final Answer:

    Pure pipes run only when input references change, impure pipes run every change detection cycle. -> Option C
  4. Quick Check:

    Pure vs impure pipe execution = A [OK]
Hint: Pure pipes run less often; impure pipes run every cycle [OK]
Common Mistakes:
  • Confusing when pure and impure pipes run
  • Thinking pure pipes run every cycle
  • Assuming impure pipes only run on input change
2. Which of the following is the correct way to declare an impure pipe in Angular?
easy
A. @Pipe({ name: 'examplePipe', pure: false })
B. @Pipe({ name: 'examplePipe', impure: true })
C. @Pipe({ name: 'examplePipe' })
D. @Pipe({ name: 'examplePipe', pure: true })

Solution

  1. Step 1: Recall pipe metadata syntax

    Angular pipes use the @Pipe decorator with a 'pure' property to indicate purity.
  2. Step 2: Identify impure pipe declaration

    Setting 'pure: false' marks the pipe as impure, causing it to run every change detection cycle.
  3. Final Answer:

    @Pipe({ name: 'examplePipe', pure: false }) -> Option A
  4. Quick Check:

    Impure pipe uses pure: false [OK]
Hint: Impure pipes set pure to false in @Pipe decorator [OK]
Common Mistakes:
  • Using pure: true for impure pipes
  • Omitting pure property defaults to pure
  • Using non-existent 'impure' property
3. Given this Angular pipe usage:
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?
medium
A. The pipe will run and update the output to include 4.
B. Angular throws an error due to mutation.
C. The pipe runs only if change detection is manually triggered.
D. The pipe will not run; output remains unchanged.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The pipe will not run; output remains unchanged. -> Option D
  4. Quick Check:

    Pure pipe ignores internal mutations = C [OK]
Hint: Pure pipes need new object references to update [OK]
Common Mistakes:
  • Assuming mutation triggers pure pipe
  • Thinking Angular throws error on mutation
  • Believing manual change detection is required here
4. You have an Angular pipe declared as @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?
medium
A. The pipe is impure and runs too often causing errors.
B. The pipe is pure and input array reference does not change on filtering.
C. The pipe is missing the transform method.
D. The pipe is declared without a name.

Solution

  1. Step 1: Identify pipe purity and input behavior

    A pure pipe runs only when the input reference changes, not when the content changes.
  2. 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.
  3. Final Answer:

    The pipe is pure and input array reference does not change on filtering. -> Option B
  4. Quick Check:

    Pure pipe needs new input reference to update [OK]
Hint: Pure pipes need new input references to update output [OK]
Common Mistakes:
  • Assuming impure pipe causes this issue
  • Forgetting to return new array reference
  • Ignoring pipe naming or method errors
5. You want to create a pipe that formats the current time every second in Angular. Which pipe type should you use and why?
hard
A. Impure pipe, because it runs every change detection cycle and updates time frequently.
B. Pure pipe, because it runs only when input changes, improving performance.
C. Pure pipe, because it automatically triggers updates every second.
D. Impure pipe, because pure pipes cannot be used for formatting.

Solution

  1. Step 1: Understand the need for frequent updates

    Formatting current time every second requires the pipe to run frequently to update the display.
  2. 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.
  3. Final Answer:

    Impure pipe, because it runs every change detection cycle and updates time frequently. -> Option A
  4. Quick Check:

    Frequent updates need impure pipe [OK]
Hint: Use impure pipes for frequent, automatic updates [OK]
Common Mistakes:
  • Choosing pure pipe for frequent updates
  • Thinking pure pipes auto-update every second
  • Confusing pipe purpose with formatting capability
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard