Friday, November 30, 2018

Pure vs Impure pipes

Angular pipes execute based on changes detected to the input value to which the pipe is applied. Based on how the changes should be detected there are 2 types of pipes Pure and Impure pipes.

By default pipes are pure in Angular. Pure pipes get executed when there is a change to the input value to the pipe. These changes include changes to primitive types like String, Number, Boolean etc, and changes to object references i.e when assignment happens to object, arrays etc.

Note that pure pipes get triggered only when the reference of the object/array is changed. Pure pipes will not get triggered when we add an element to an existing array (or) when we update a property of an input object, but will get executed when the input array is assigned with a different array or when an input object is assigned with a different object. When we add an element to the existing array the reference remains the same hence the pipe will not get executed.

Impure pipes get executed for every change detected to the component like a key press, mouse move etc. Impure pipes get executed more often when compared to pure pipes. Hence impure pipes should be implemented with greater care.

In some cases pure pipes might not solve the need, for example if we want a pipe to get executed based on addition/removal of elements from an array, then a pure pipe will not work, in these cases we should develop an impure pipe.


Search Flipkart Products:
Flipkart.com

No comments: