Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Angular pure pipe

Suggested Videos
Part 45 - Angular form reset | Text | Slides
Part 46 - Javascript objects and reference variables | Text | Slides
Part 47 - Filter pipe in angular | Text | Slides

In this video we will discuss
  • What is a pure pipe
  • Why is it not recommended to use pipes to filter and sort data in Angular

This is continuation to Part 47, please watch Part 47 from Angular CRUD tutorial before proceeding.

In Angular, there are two categories of pipes
  • Pure Pipes
  • Impure Pipes

When you create a new pipe, it is pure by default. To make a pipe impure, set it's pure flag to false. Impure pipes can significantly affect the performance of the application. So you have to think very carefully, before you use an impure pipe in your angular application. We will discuss impure pipes and why they are bad from performance perspective in our next video.

@Pipe({
    name: 'employeeFilter',
    pure: false
})
export class EmployeeFilterPipe implements PipeTransform {

Pure pipe
  • Is fast
  • Angular executes a pure pipe only when it detects a pure change to the input value
  • A pure change is either a change to a primitive input value (String, Number, Boolean) or a changed object reference (Array, Date, Object)
  • A pure pipe is not executed if the input to the pipe is an object and only the property values of the object change but not the reference
Why are Pure pipes fast 
  • An input for a pipe can either be a value type (String, Number, Boolean etc) or a reference type (like Array, Date, Object etc.)
  • If the input to the pure pipe is a value type. Since value types store data directly in the memory slot comparing if a value type value has changed is very quick.
  • On the other hand, if the input to the pure pipe is a reference type, it is executed only if the reference of the input object has changed. Checking if an object reference has changed is much faster than checking if each of the object individual property values have changed.
So pure pipes are fast, but using them for filtering data is not a good idea because, the filtering may not work as expected if the source data is updated without a change to the object reference.

One way to make this work is by making the pipe impure. Impure pipes can significantly impact the performance of the application as they run on every change detection cycle.

We will discuss impure pipes and the performance impact they can have in our next video.

angular crud tutorial

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.