Skip to content

perf(docs-infra): remove usage of the NgClass directive - #48312

Closed
pkozlowski-opensource wants to merge 1 commit into
angular:mainfrom
pkozlowski-opensource:aio_inp_ngclass
Closed

pkozlowski-opensource wants to merge 1 commit into
angular:mainfrom
pkozlowski-opensource:aio_inp_ngclass

Conversation

@pkozlowski-opensource

@pkozlowski-opensource pkozlowski-opensource commented Dec 1, 2022 •

Copy link
Copy Markdown
Member

NgClass has non-negligable performance cost, especially if a
different object is provided as NgClass input (which was the case
for the NavItemComponent). The perfornace problem was noticed
in the scope of the INP score investigations while profiling the
https://angular.io/resources page. On this particular page replacing
NgClass usage with alternatives results in 20x (!) runtime perf
improvement. Such big improvement is possible since we avoid unneeded
CSS classes removal / adding in the DOM.

Part of #25518

Before:
Screen Shot 2022-12-01 at 16 10 18

By removing calls to ngClass calls that suffer from the problem described here (see experiment in #48312) we are seeing 20x spped improvement (the rendering time goes down to ~16ms):
Screen Shot 2022-12-01 at 16 10 35

@ngbot ngbot Bot modified the milestone: Backlog Dec 1, 2022
@mary-poppins

Copy link
Copy Markdown

You can preview 5419936 at https://pr48312-5419936.ngbuilds.io/.

@mary-poppins

Copy link
Copy Markdown

You can preview 5419936 at https://pr48312-5419936.ngbuilds.io/.

@mary-poppins

Copy link
Copy Markdown

You can preview a735499 at https://pr48312-a735499.ngbuilds.io/.

@mary-poppins

Copy link
Copy Markdown

You can preview 3d7f012 at https://pr48312-3d7f012.ngbuilds.io/.

@mary-poppins

Copy link
Copy Markdown

You can preview 494f1d0 at https://pr48312-494f1d0.ngbuilds.io/.

NgClass has non-negligable performance cost, especially if a
different object is provided as NgClass input (which was the case
for the NavItemComponent). The perfornace problem was noticed
in the scope of the INP score investigations while profiling the
https://angular.io/resources page. On this particular page replacing
NgClass usage with alternatives results in 20x (!) runtime perf
improvement. Such big improvement is possible since we avoid unneeded
CSS classes removal / adding in the DOM.

Part of angular#25518
@pkozlowski-opensource pkozlowski-opensource added the action: review The PR is still awaiting reviews from at least one requested reviewer label Dec 2, 2022
@mary-poppins

Copy link
Copy Markdown

You can preview 692618e at https://pr48312-692618e.ngbuilds.io/.

@pkozlowski-opensource
pkozlowski-opensource marked this pull request as ready for review December 2, 2022 10:58
@pullapprove
pullapprove Bot requested a review from josephperrott December 2, 2022 10:58
@pkozlowski-opensource pkozlowski-opensource added the target: patch This PR is targeted for the next patch release label Dec 2, 2022
@pkozlowski-opensource pkozlowski-opensource changed the title perf(docs-infra): test ngClass theory perf(docs-infra): remove usage of the NgClass directive Dec 2, 2022
@pkozlowski-opensource pkozlowski-opensource self-assigned this Dec 2, 2022

@josephperrott josephperrott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the one comment about a potential anti-pattern, but if I am wrong/we want to go with it then its good to go.

@@ -1,25 +1,25 @@
<div *ngIf="!node.children">
<a href="{{node.url}}" [ngClass]="classes" title="{{node.tooltip}}"
<a href="{{node.url}}" [class]="classes" title="{{node.tooltip}}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly we typically consider having a binding on [class] as an antipattern, as opposed to the specific class.member item. This makes it so that if another directive is involved that affects classes it gets clobbered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of ivy we do merge class bindings from different sources so the clobbering is no longer the issues. I was considering using individual class.xyz bindings, but:

  • we do need to have "calculated" name (level-x) anyway;
  • the same set of classes is bound in 3 different places in this template so it would be more code duplication.

I see the current approach as the "least bad"...

@pkozlowski-opensource pkozlowski-opensource added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Dec 2, 2022
@AndrewKushnir

Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit 6706fab.

AndrewKushnir pushed a commit that referenced this pull request Dec 2, 2022
NgClass has non-negligable performance cost, especially if a
different object is provided as NgClass input (which was the case
for the NavItemComponent). The perfornace problem was noticed
in the scope of the INP score investigations while profiling the
https://angular.io/resources page. On this particular page replacing
NgClass usage with alternatives results in 20x (!) runtime perf
improvement. Such big improvement is possible since we avoid unneeded
CSS classes removal / adding in the DOM.

Part of #25518

PR Close #48312
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jan 2, 2023
trekladyone pushed a commit to trekladyone/angular that referenced this pull request Feb 1, 2023
NgClass has non-negligable performance cost, especially if a
different object is provided as NgClass input (which was the case
for the NavItemComponent). The perfornace problem was noticed
in the scope of the INP score investigations while profiling the
https://angular.io/resources page. On this particular page replacing
NgClass usage with alternatives results in 20x (!) runtime perf
improvement. Such big improvement is possible since we avoid unneeded
CSS classes removal / adding in the DOM.

Part of angular#25518

PR Close angular#48312
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: performance Issues related to performance target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants