A personal injury settlement mill is a high volume law practice that attempts to mass produce the settlement of injury claims while utilizing aggressive advertising campaigns in an effort to obtain…
The Angular framework has a very neat way to control the component template structure. This is done by, so called, structure directives, e.g. *ngIf, *ngForOf, *ngSwitch. In this post, we will concentrate on the *ngForOf directive, because this is the mainstream way of rendering an iterable data in Angular. Sometimes, however, this could be slow.
For those who want to cut to the chase and see the proposed optimization, jump to the last section of the post. For the rest of us, lets keep reading and briefly discuss the default *ngForOf implementation.
As we can see, *ngForOf
will update the DOM on every change. By default, it will compare list items by reference, which won't be efficient if an immutable data is used. Basically, a change will be detected when the item reference updates, even though that the item structure or values are still the same. Lets examine the following Angular demo:
Everything looks OK, right? Uhh… no, not exactly. The trackBy
function won’t help when the data has actually changed and we use custom components. In this case, the *ngForOf
directive will destroy the old component and will create a new one for every change.
Notice how that the whole <li>
is recreated on change. Basically, the directive will remove the old DOM element and will add new one even though only the dataItem.value
has changed.
As you can see, we don’t do anything fancy here. We just want to:
The first thing that we came up with was to ‘unfold’ the loop and use N-times *ngIf
directives. This requires to copy the template n-times and pass every data item by index. If you can’t image it, I don’t blame you, it is not the brightest idea.
Surprisingly enough, this works, because DOM nodes are not removed, and only the corresponding bindings are updated. If the displayed data has a fixed maximum length, e.g. 30 items, then the duplicated templates with *ngIf
could be suitable solution.
The main concern here is the size of the template. This will slow down the compilation (a real nightmare for your CI) and will produce bigger JS footprint.
The benefits are obvious:
The drawback is that the item change cannot be animated using an enter or leave animation.
We found out that Angular could be tweaked to render even faster. Even though that the proposed custom implementation has its limitations, it gives 30–50% improvementment of the rendering time. I will skip animations all day long if the component renders faster.
This is an audio piece I covered on what it’s like being a foreign student living in Canada.. “International Student Struggles” is published by Jordanna Cooper in Jay the Journalist.
Con sciarpa e cappello sono uscita. “Ogni maledetta mattina invernale” is published by Annie.
The ability to adjust oneself to different conditions is known as adaptability. Being adaptable is an excellent skill that one can acquire. Adaptability is a skill that I have learned over the past…