A pure pipe is a pipe that Angular executes only when it detects a pure change to the input value. Pure changes include changes to primitive types or object references. Pure pipes improve performance by avoiding unnecessary recalculations.
Click to reveal answer
beginner
What is an impure pipe in Angular?
An impure pipe is a pipe that Angular executes on every change detection cycle, regardless of whether the input value changed. This is useful when the pipe depends on mutable objects or external state.
Click to reveal answer
intermediate
How do you declare a pipe as impure in Angular?
You declare a pipe as impure by setting the 'pure' property to false in the @Pipe decorator, like this: @Pipe({name: 'myPipe', pure: false}).
Click to reveal answer
intermediate
Why should you avoid using impure pipes when possible?
Impure pipes run on every change detection cycle, which can hurt performance. Using pure pipes when possible helps Angular skip unnecessary work and keeps apps fast.
Click to reveal answer
advanced
Give an example scenario where an impure pipe is necessary.
When a pipe transforms data from a mutable object like an array that changes internally (e.g., items added or removed without changing the array reference), an impure pipe ensures the view updates correctly.
Click to reveal answer
When does Angular execute a pure pipe?
AWhen the user clicks a button
BOn every change detection cycle
COnly when the input reference or primitive value changes
DOnly once when the component loads
✗ Incorrect
Pure pipes run only when Angular detects a pure change to the input, such as a new object reference or a changed primitive value.
How do you mark a pipe as impure in Angular?
AUse @ImpurePipe decorator
BSet pure: false in the @Pipe decorator
CSet pure: true in the @Pipe decorator
DNo special setting needed
✗ Incorrect
You mark a pipe as impure by setting pure: false in the @Pipe decorator.
What is a downside of using impure pipes?
AThey run on every change detection cycle, which can reduce performance
BThey never update the view
CThey only work with primitive values
DThey require manual triggering
✗ Incorrect
Impure pipes run every change detection cycle, which can slow down the app if overused.
Which pipe type is best for transforming immutable data?
APure pipe
BImpure pipe
CAsync pipe
DNo pipe
✗ Incorrect
Pure pipes are ideal for immutable data because they only run when the input changes.
If you have an array that changes internally but keeps the same reference, which pipe should you use?
APure pipe
BNo pipe
CAsync pipe
DImpure pipe
✗ Incorrect
Impure pipes detect internal changes in mutable objects even if the reference stays the same.
Explain the difference between pure and impure pipes in Angular and when to use each.
Think about how Angular detects changes and when it runs pipes.
You got /4 concepts.
Describe how to declare an impure pipe and why you might need one.
Focus on the decorator setting and use case.
You got /4 concepts.
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
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 C
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
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 A
Quick Check:
Impure pipe uses pure: false [OK]
Hint: Impure pipes set pure to false in @Pipe decorator [OK]
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
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 D
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
Step 1: Identify pipe purity and input behavior
A pure pipe runs only when the input reference changes, not when the content changes.