← Back to list

Understanding the difference between Pure and Impure Pipes in Angular: When to use which?

Learn about the advantages and limitations of pure and impure pipes and how to choose the right one for your Angular project

An Tran · 2023-01-27 14:49 · 1 claps · 2.5 min read
#angular-pipe #pure-pipe #impure-pipe #angular-performance #angular-development
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Understanding the difference between Pure and Impure Pipes in Angular: When to use which?

Pipes are an essential feature in Angular that allow you to transform data in your templates. They are simple to use, and they can help you keep your code clean and organized. However, there are two types of pipes in Angular: pure and impure. In this article, we will explore the difference between pure and impure pipes, and when to use each one.

First, let’s start with pure pipes. A pure pipe is a pipe that only runs when its input or parameters change. They are typically used for simple data transformations, such as formatting a date or currency. One of the main advantages of pure pipes is that they improve the performance of your application. Since they only run when their input changes, they are less likely to cause performance issues.

An example of using a pure pipe in Angular is the built-in date pipe. The date pipe takes a JavaScript Date object and formats it as a string. You can use it in your templates like this:

Copy code
<p>{{ myDate | date }}</p>

In this example, the date pipe will only run when myDate changes. If myDate stays the same, the pipe will not run again.

On the other hand, impure pipes are pipes that run on every change detection cycle, regardless of whether their input or parameters have changed. They are typically used for more complex data transformations, such as filtering an array. The main advantage of impure pipes is that they are more flexible and can handle more complex logic.

An example of using an impure pipe in Angular is the filter pipe. The filter pipe takes an array and a search term, and returns a new array with only the items that match the search term. You can use it in your templates like this:

Copy code
<li *ngFor="let item of myArray | filter: searchTerm">{{ item }}</li>

In this example, the filter pipe will run on every change detection cycle, regardless of whether myArray or searchTerm have changed.

When deciding whether to use a pure or impure pipe, you should consider the complexity of the data transformation and the performance of your application. Pure pipes are more performant and should be used for simple data transformations, while impure pipes are more flexible and should be used for more complex data transformations.

In conclusion, pipes are a powerful feature in Angular that can help you transform data in your templates. Pure pipes are best used for simple data transformations and impure pipes are best used for more complex data transformations. When choosing between the two, consider the complexity of the data transformation and the performance of your application. To learn more about pipes in Angular, check out the official documentation.

Here is an example of a pure pipe in Angular:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'purePipe',
    pure: true
})
export class PurePipe implements PipeTransform {
    transform(value: any, args?: any): any {
        // Perform simple data transformation
        return value.toUpperCase();
    }
}

This pipe converts the input value to uppercase and it will only run when the input value changes.

Here is an example of an impure pipe in Angular:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'impurePipe',
    pure: false
})
export class ImpurePipe implements PipeTransform {
    transform(value: any, args?: any): any {
        // Perform complex data transformation
        return value.filter(item => item.active === true);
    }
}

This pipe filters the input array to only return items that have an active property set to true, it will run on every change detection cycle, regardless of whether the input value has changed or not.

You can use these pipes in your templates like this:

<p>{{ myValue | purePipe }}</p>
<li *ngFor="let item of myArray | impurePipe">{{ item }}</li>

It’s important to note that the pure: true and pure: false are optional, by default all pipes are pure.


메타데이터
post_id
9bc1b28076ec
slug
understanding-the-difference-between-pure-and-impure-pipes-in-angular-when-to-use-which-9bc1b28076ec
url
https://medium.com/@tranan.aptech/understanding-the-difference-between-pure-and-impure-pipes-in-angular-when-to-use-which-9bc1b28076ec
canonical_url
https://medium.com/@tranan.aptech/understanding-the-difference-between-pure-and-impure-pipes-in-angular-when-to-use-which-9bc1b28076ec
author_url
https://medium.com/@tranan.aptech
status
ok
fetched_at
2026-07-13 18:09:28