ANGULAR INTERVIEW QUESTION: What is the difference between pure and impure pipes?
๐ก ๐๐๐๐ ๐๐๐๐๐
Pure pipes are stateless, which means that there is no handling of a state between two calls, and there's no collateral effect.
Hence, a pure pipe is called only when Angular detects a change in the value parsed to the pipe.
uppercase.pipe.ts
<โ1>{{ ๐ก๐๐ก๐๐ | ๐ข๐๐๐๐๐๐๐ ๐ }}
๐ก ๐๐๐๐๐๐ ๐๐๐๐๐
On the other hand, Angular calls an impure pipe for each change detection cycle, no matter the value could change.
This can be useful in some cases when usual pipe changes can't be detected by Angular:
๐ When we parse to the pipe an array or an object whom content did not change, while the instance is still the same.
๐ When your pipe injects a service to access other values, but Angular has no way to detect the changes and does not update the view.
In such cases, using an impure pipe is meaningful, because your pipe is executed and your display updated.
But remember that impure pipes may be less efficient.
For example, when an array is parsed to an impure pipe to filter or sort the data, then the processing could be executed each time the change detection runs, even if the array did not change.
In that case, your pipe should try to detect this and send data from a cache.
๐บ๐ ๐ ๐๐๐๐ ๐๐๐๐ ๐๐ ๐๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐ ๐จ๐๐๐๐๐๐ ๐ ๐๐๐๐๐๐ ๐ ๐๐๐๐๐๐ ๐๐ ๐๐๐ ๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐ ๐๐๐ ๐๐๐๐.
๐จ๐ ๐๐๐๐๐๐ ๐๐๐๐ ๐๐ ๐๐๐๐๐๐ ๐๐๐ ๐๐๐๐ ๐๐๐๐๐๐ ๐ ๐๐๐๐๐๐๐๐ ๐๐๐๐๐, ๐๐๐๐๐๐๐๐ ๐๐๐ ๐๐๐๐๐ ๐๐๐๐๐๐๐. ๐ฎ๐๐๐๐๐๐๐๐, ๐๐ ๐๐๐๐๐ ๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐๐๐๐ ๐๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐๐๐๐ ๐๐๐๐๐๐๐๐๐๐.