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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
LetDirectiveas follows:Alternatively, we could set the properties using the
PushPipewithout using a ViewModel in the first place:... a third approach would be to use the
LetDirectiveon a per property basis:Angular and NgRx Insights
If we take a closer look at both the
PushPipeand theLetDirective, one might come to the conclusion that theLetDirectiveis typically a bit faster. This has two reasons:PushPipeis not pureOur three examples above produce the following JavaScript outputs. First, the ViewModel approach:
Second, the
PushPipeper property approach:... and third, the
LetDirectiveper property approach:Regarding the source code, I would consider the
PushPipeper property approach to be the "slowest" of these three, followed by theLetDirectiveper property approach and the fastest being the ViewModel approach. Regarding property binding, we can see that Angular handles the binding of thePushPipenatively in the template, while theLetDirectivedoes 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:LetDirectiveforce a rerender of every child element inside the element theLetDirectiveis placed on?LetDirectiveand thePushPipeboth 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.usernamechanges in ourvm(regarding our example above), does Angular have to checkstatusaswell? 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