Pure vs Impure Pipes in Angular

πŸ’‘ Concept Name

Pure vs. Impure Pipes

πŸ“˜ Quick Intro

Pipes in Angular transform data in templates. Pure pipes run only when input reference changes, while impure pipes run on every change detection cycle.

🧠 Analogy / Short Story

Think of a pure pipe like a light switchβ€”it only flips when you ask. An impure pipe is like a motion sensorβ€”it checks constantly, even if you’re standing still.

πŸ”§ Technical Explanation

  • βœ… Pure Pipes:
    • Stateless and side-effect-free.
    • Called only when input reference changes.
    • Optimal for performance (checked infrequently).
  • ❌ Impure Pipes:
    • May have internal state or side effects.
    • Called on every change detection cycle.
    • Can degrade performance if overused.

🎯 Purpose & Use Case

  • βœ… Use pure pipes for formatting, casing or simple calculations.
  • βœ… Use impure pipes for filtering, sorting arrays in templates.
  • βœ… Avoid heavy impure pipes in large lists to maintain performance.

πŸ’» Real Code Example

// pure-counter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

                    @Pipe({ name: 'pureCounter' })
export class PureCounterPipe implements PipeTransform {
  transform(value: number): number {
    return value * 2;
  }
}

// impure-filter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

                    @Pipe({ name: 'impureFilter', pure: false })
export class ImpureFilterPipe implements PipeTransform {
  transform(items: any[], search: string): any[] {
    return items.filter(i => i.name.includes(search));
  }
}

❓ Interview Q&A

Q1: What does `pure: false` do in @Pipe decorator?
A: Marks the pipe as impure to run on every change detection.

Q2: When should you avoid impure pipes?
A: When rendering large lists or complex views.

Q3: Are pure pipes cached by Angular?
A: Yes, until input reference changes.

Q4: Can pure pipes have side effects?
A: No, they should be side-effect-free.

Q5: How do you optimize filtering instead of using impure pipes?
A: Pre-filter data in component and use pure pipes or built-in directives.

πŸ“ MCQs

Q1. When is a pure pipe called?

  • Every cycle
  • When input reference changes
  • On init only
  • Never

Q2. What is the default purity of pipes?

  • Impure
  • Pure
  • Neither
  • Both

Q3. Which decorator flag makes a pipe impure?

  • impure: true
  • pure: false
  • stateful: true
  • detect: always

Q4. What can cause performance issues?

  • Pure pipes on static data
  • Impure pipes on large lists
  • No pipes
  • Both

Q5. How to improve performance over impure pipes?

  • Use more pipes
  • Filter in component
  • Use impure pipes
  • Use pure pipes only

Q6. Can impure pipes be stateful?

  • No
  • Yes
  • Only pure pipes
  • Only custom pipes

Q7. Which pipes run least frequently?

  • Impure pipes
  • Pure pipes
  • All pipes
  • No pipes

Q8. Why cache pure pipes?

  • To slow down app
  • To avoid recomputation
  • To use more memory
  • To detect changes

Q9. What is change detection in Angular?

  • Garbage collection
  • Process checking for updates
  • Route changes
  • DI instantiation

Q10. Which is side-effect-free?

  • Impure pipe
  • Pure pipe
  • Both
  • None

πŸ’‘ Bonus Insight

Use memoization or RxJS operators in components to handle dynamic data rather than impure pipes to keep templates performant.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More About Angular πŸš€

What is Angular vs AngularJS πŸ‘‰ Explained
Key Features of Angular πŸ‘‰ Explained
Angular Architecture πŸ‘‰ Explained
Components in Angular πŸ‘‰ Explained
Modules in Angular πŸ‘‰ Explained
Templates in Angular πŸ‘‰ Explained
Directives in Angular πŸ‘‰ Explained
Structural vs Attribute Directives πŸ‘‰ Explained
Services in Angular πŸ‘‰ Explained
Dependency Injection in Angular πŸ‘‰ Explained
Data Binding in Angular πŸ‘‰ Explained
Interpolation πŸ‘‰ Explained
Property Binding πŸ‘‰ Explained
Event Binding πŸ‘‰ Explained
Two-way Data Binding πŸ‘‰ Explained
ngModel Usage πŸ‘‰ Explained
Lifecycle Hooks πŸ‘‰ Explained
ngOnInit Hook πŸ‘‰ Explained
ngOnDestroy Hook πŸ‘‰ Explained
Pipes in Angular πŸ‘‰ Explained
Pure vs Impure Pipes πŸ‘‰ Explained
Create Custom Pipe πŸ‘‰ Explained
Angular Forms πŸ‘‰ Explained
Template-driven vs Reactive Forms πŸ‘‰ Explained
Form Controls πŸ‘‰ Explained
Form Validation πŸ‘‰ Explained
Validation Error Messages πŸ‘‰ Explained
FormGroup vs FormControl πŸ‘‰ Explained
Enable/Disable Form Fields πŸ‘‰ Explained
Reset Form πŸ‘‰ Explained
Routing in Angular πŸ‘‰ Explained
RouterModule πŸ‘‰ Explained
Configure Routes πŸ‘‰ Explained
Route Guards πŸ‘‰ Explained
CanActivate vs CanDeactivate πŸ‘‰ Explained
Lazy Loading πŸ‘‰ Explained
Preloading in Routing πŸ‘‰ Explained
Wildcard Routes πŸ‘‰ Explained
Route Parameters πŸ‘‰ Explained
Query Parameters πŸ‘‰ Explained
Programmatic Navigation πŸ‘‰ Explained
routerLink & routerLinkActive πŸ‘‰ Explained
Resolvers in Routing πŸ‘‰ Explained
Observables in Angular πŸ‘‰ Explained
RxJS in Angular πŸ‘‰ Explained
Observable vs Promise πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup