Skip to content

ngClass causes unnecessary CSS class mutation even if computed classes are unchanged #25518

Closed

Description

@pkozlowski-opensource

I'm submitting a...


[x] Bug report  

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-jxxkvh?file=src%2Fapp%2Fapp.component.ts

Steps to reproduce:

  • open DevTools "Elements" tab and inspect the <div> with the I'm red content
  • click on the I'm just triggering CD button and observe that the red CSS class gets removed and added just after.

Current behavior

CSS class is removed and added just after, even if a set of CSS classes doesn't change based on the model provided.

Expected behavior

There are no unnecessary CSS class mutations.

What is the motivation / use case for changing the behavior?

Current behaviour causes unnecessary DOM access impacting overall performance.
It can also tip off people using mutation observers as reported in ng-bootstrap/ng-bootstrap#2582 (comment)

Environment

Angular version: ALL
Browser: ALL

Additional info

This happens because I'm giving ngClass a new object on each CD. NgClass / differs assume that a new object might have different shape and removes classes from the old object. We should probably be smarter about it...

Activity

  1. added
    area: common
    on Aug 16, 2018
  2. added this to the needsTriage milestone on Aug 16, 2018
  3. added
    area: performance
    and removed
    area: performance
    on Aug 16, 2018
  4. modified the milestones: needsTriage, Backlog on Apr 25, 2019
  5. changed the title [-]ngCLass causes unnecessary CSS class mutation in some cases[/-] [+]ngClass causes unnecessary CSS class mutation even if computed classes are unchanged[/+] on May 28, 2020
  6. 9 remaining items

  7. added a commit that references this issue on Jan 13, 2023
    032b2bd
  8. angular-automatic-lock-bot commented on Feb 13, 2023

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: commonarea: performancefreq2: mediumstate: confirmedtype: bug/fixworkaround1: obvious

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions