Hien's Home
View RSS feed

How pure and impure pipes work in Angular Ivy

Published on

Introduction

Angular's piping mechanism is something Angular developers use everyday. There's an excellent article that explores pipes in depth, and the gist of the article is the following:

When pipe is pure, transform() method is invoked only when its input arguments change. Pipes are pure by default. If the pipe has internal state (that is, the result depends on the state other than its arguments), set pure to false. In this case, the pipe is invoked on each change detection cycle, even if the arguments have not changed.

Another interesting feature of the pure pipes is that Angular creates only one instance of a pure pipe regardless of how many times a pipe is used in a template:

1<span [text]="value | myCustomPurePipe">
2<span [text]="value | myCustomPurePipe">

Here only one instance of myCustomPurePipe should be created.

This was true before Ivy, and in this article I'm going to take a look under the hood to find out if this still holds true in Ivy.

Setting things up

First thing first, let's create a new Angular project with version ≥ 9, because we are exploring pipes in Ivy implementation.

1ng new study-pipes --minimal
2cd study-pipes

Create a custom pure and an impure pipe

Next we will create a custom pipe named my-custom-pure-pipe and my-custom-impure-pipe:

1ng g p my-custom-pure-pipe --skip-tests
2ng g p my-custom-impure-pipe --skip-tests

Then, change the implementation of my-custom-pure-pipe like this:

1@Pipe({
2 name: 'myCustomPurePipe',
3 pure: true
4})
5export class MyCustomPurePipe implements PipeTransform {
6 constructor() {
7 console.log('MyCustomPurePipe created');
8 }
9
10 transform(value: number, ...args: any[]): any {
11 console.log(`MyCustomPurePipe#transform called, value ${value}`);
12 return value;
13 }
14}

And change my-custom-impure-pipe like this:

1@Pipe({
2 name: 'myCustomImpurePipe',
3 pure: false
4})
5export class MyCustomImpurePipe implements PipeTransform {
6 constructor() {
7 console.log('MyCustomImpurePipe created');
8 }
9
10 transform(value: number, ...args: any[]): any {
11 console.log(`MyCustomImpurePipe#transform called, value ${value}`);
12 return value + value;
13 }
14}

Basically here we're just logging at the instance creation stage and when the transform is called by Angular during change detection.

In app.component.ts, change the code as below:

1@Component({
2 selector: 'app-root',
3 templateUrl: './app.component.html',
4 styleUrls: ['./app.component.css']
5})
6export class AppComponent {
7 number1 = 1;
8 number2 = 2;
9}

And in angular.json file, change aot option at projects -> study-pipes -> architect -> build -> options -> aot from true to false to disable ahead of time (AOT) compilation. This will allow us to explore the generated code more easily.

Now we're finished with setting up the project. Let's start our exploration journey.

Exploring Pipes

Suppose in the app.component.html we have the following template

1<span>{{ number1 | myCustomPurePipe }}</span>
2<span>{{ number2 | myCustomPurePipe }}</span>
3<span>{{ number1 | myCustomImpurePipe }}</span>
4<span>{{ number2 | myCustomImpurePipe }}</span>

Let's open the Chrome dev tools, in Source tab, and navigate to app component file, you will see this code:

image.png

There are two main if blocks in the generated code inside AppComponent_Template: the code executed during component instantiation rf & 1 and change detection logic rf & 2.

Here is the creation block:

1if (rf & 1) { // this is the creation phase
2 jit___elementStart_2(0,'span');
3 jit___text_3(1);
4 jit___pipe_4(2,'myCustomPurePipe'); // pipe instance is created
5 jit___elementEnd_5();
6 jit___elementStart_2(3,'span');
7 jit___text_3(4);
8 jit___pipe_4(5,'myCustomPurePipe'); // pipe instance is created
9 jit___elementEnd_5();
10 jit___elementStart_2(6,'span');
11 jit___text_3(7);
12 jit___pipe_4(8,'myCustomImpurePipe'); // pipe instance is created
13 jit___elementEnd_5();
14 jit___elementStart_2(9,'span');
15 jit___text_3(10);
16 jit___pipe_4(11,'myCustomImpurePipe'); // pipe instance is created
17 jit___elementEnd_5();

The jit__pipe_4 is actually the function to create a new instance of a pipe. So you can see that we'll have 4 instances of pipes. It means that in Ivy every pipe has its own instance, be it a pure or impure pipe. Whereas in View Engine, pure pipe has a shared instance.

Let's now look at the change detection block:

1if (rf & 2) {
2 jit___advance_6(1);
3 jit___textInterpolate_7(jit___pipeBind1_8(2,4,ctx.number1));
4 jit___advance_6(3);
5 jit___textInterpolate_7(jit___pipeBind1_8(5,6,ctx.number2));
6 jit___advance_6(3);
7 jit___textInterpolate_7(jit___pipeBind1_8(8,8,ctx.number1));
8 jit___advance_6(3);
9 jit___textInterpolate_7(jit___pipeBind1_8(11,10,ctx.number2));
10}

The jit___pipeBind1_8 is the function to call transform on a pipe.

Here's the code when the pipe does the transform task.

1// this code is called in update phase, or when change detection runs
2export function ɵɵpipeBind1(index: number, slotOffset: number, v1: any): any {
3 const adjustedIndex = index + HEADER_OFFSET;
4 // get LView, LView stands for Logical View
5 const lView = getLView();
6
7 // get pipeInstance from LView
8 const pipeInstance = load<PipeTransform>(lView, adjustedIndex);
9
10 return unwrapValue(
11 lView,
12 // whether pipe is pure
13 isPure(lView, adjustedIndex)
14 // call pipe’s transform method or return from cache value
15 ? pureFunction1Internal(
16 lView, getBindingRoot(), slotOffset, pipeInstance.transform, v1, pipeInstance)
17 // pipe is impure then call pipe’s transform method directly
18 : pipeInstance.transform(v1));
19}

From the above code, the isPure method will check whether a pipe is pure or impure by looking at the pure property in @Pipe decorator.

For impure pipes Angular calls the transform method on every change detection. For any input change to the pure pipe, it will call transform function. Otherwise it will return a cached value.

Conclusion

So, to conclude:

  • In Ivy, every pipe has its own instance, be it a pure or impure pipe. Whereas in View Engine, pure pipe has a shared instance. For example, in Ivy, if I use myCustomPurePipe in two places in a template, then, two instances of MyCustomPurePipe are created.
  • In a component which uses Default change detection strategy, when change detection happens, if the pipe is impure, then the transform method will be called. If the pipe is pure, whether there are any changes in input parameters in the transform method from the last call, then transform method will be called. Otherwise the pipe will return the cached value from last transform call.
  • When using impure pipe async, you should use it together with OnPush change detection to avoid unnecessary calls to transform on every change detection.

Here are two StackBlitz repos for you to play around, one is for View Engine, and another is for Ivy.

If you would like to explore more aspect of Angular Ivy, like new debugging tools, new APIs as well as language syntax, I highly recommend the book Accelerating Angular Development with Ivy by Lars Gyrup Brink Nielsen and Jacob Andresen.

Thanks for reading and happy coding!