I've always believed that slice
is pure and it's sole advantage over calling slice
method on string or array is caching mechanism of pure pipes in Angular.
Turns out it isn't the case and slice
is impure. Moreover implementation is very basic: it guards against null
s and delegates to slice
method. No internal comparison of last transformed input.
I suspect it is designed to align with behavior of ngFor
, but is it a viable reason? Newcomers would complain that Angular is broken otherwise I suppose, but then again why should I use this pipe?
Am I correct that it creates new arrays in every change detection run, which leads to triggering even OnPush
change detection down the three and nullifying performance optimizations?
Edit: This question is based on assumption that one should use immutable data.
This pipe is impure because it would otherwise return the same result until the array reference changes, so it wouldn't work when the array items change.
I personally think it is bad design to use such pipes. They removed the FilterPipe because of performance issues. Indeed, a pipe is triggered on every change detection, and there are many! I think the slice pipe should be removed as well. For me it's a quick and dirty solution, that brings more problems than it solves.
I suggest you slice your arrays yourself when needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With