Performance considerations regarding the LetDirective and the PushPipe #4177
Replies: 1 comment
-
After reading through some of the Angular Discussions, debugging Angular's change detection in a sample project and reading the
In my tests,
|
Beta Was this translation helpful? Give feedback.
-
Hello everyone,
I am currently (hyper) optimizing a hybrid Cordova/Ionic app that heavily relies on NgRx. It was using AngularJS before we started the migration process to more modern technologies. Compared to our previous approach it now is considerably faster (with a big shout out to NgRx). Nonetheless, some of our bigger customers are still relying on older mobile devices. Don't get me wrong, the app is running quite well on these platforms too, but further improving performance is never a bad thing. Regardless of other performance optimization techniques such as Angular's OnPush change detection strategy, Zoneless Angular or the usage of shimmer skeleton text while loading, I want to focus on NgRx LetDirective and PushPipe here.
Problem Statement
ViewModels are a quite common pattern in NgRx, RxJS and Angular. With NgRx even supporting this pattern natively now. So I was asking myself: [Q1] "Is this the most optimal way of rendering templates considering performance?.
Consider the following (highly simplified) example. One could create a ViewModel that contains all the data required and query it using the NgRx
LetDirective
as follows:Alternatively, we could set the properties using the
PushPipe
without using a ViewModel in the first place:... a third approach would be to use the
LetDirective
on a per property basis:Angular and NgRx Insights
If we take a closer look at both the
PushPipe
and theLetDirective
, one might come to the conclusion that theLetDirective
is typically a bit faster. This has two reasons:PushPipe
is not pureOur three examples above produce the following JavaScript outputs. First, the ViewModel approach:
Second, the
PushPipe
per property approach:... and third, the
LetDirective
per property approach:Regarding the source code, I would consider the
PushPipe
per property approach to be the "slowest" of these three, followed by theLetDirective
per property approach and the fastest being the ViewModel approach. Regarding property binding, we can see that Angular handles the binding of thePushPipe
natively in the template, while theLetDirective
does that on its own. All of that, however, does not consider Angulars change detection cycle and I am unsure how to debug and determine which of these three truely is superior regarding their performance. More questions arise:LetDirective
force a rerender of every child element inside the element theLetDirective
is placed on?LetDirective
and thePushPipe
both only "mark" the component as "dirty", which causes Angular to reevaluate the component in the next application tick (i.e., change detection cycle). Angular then checks the expressions inside the template and tests if a particular expression changed. If so, it updates the DOM precisely there, where the change happened.username
changes in ourvm
(regarding our example above), does Angular have to checkstatus
aswell? I assume so, but isn't that the case for each approach anyways? As soon as a component is marked as dirty, all expressions are reevaluated, correct?I am fully aware that the example above might be too simple to answer such in-depth questions and that optimization for such simple components is out of the equation anyways, but for real world applications, this kind of knowledge might be useful considering applications that hold thousands of subscriptions at once.
Performance Best-Practices as Part of the Documentation
These kind of questions are rather difficult to answer with missing knowledge of framework and library implementation details. Information on the internet is also quite scarce. Thus, I would like to suggest the addition of a "Performance Best-Practices" page as part of NgRx's documentation.
Beta Was this translation helpful? Give feedback.
All reactions