Skip to content

Convert [ngClass]="{x: ...}" to [class.x]="..." during prod build #45734

Closed

Description

@IvanBisultanov

Which @angular/* package(s) are relevant/related to the feature request?

common, compiler

Description

I noticed that [ngClass]="..." syntax is heavier and slower than [class.x]="..." syntax.
...if I made tests in the right way =)

I did some tests here: https://github.com/IvanBisultanov/angular_bundle_size

case1 - ngClass:1_className
case2 - class.:1_className
case3 - ngClass:5_classNames
case4 - class.:5_classNames
case5 - ngClass:10_classNames_x_2000
case6 - class.:10_classNames_x_2000

These are the results:

Prod bundle size size 1/3/5 size 2/4/6 Diff
case1 vs case2 158110 158051 -59
case3 vs case4 158483 158117 -366
case5 vs case6 164574 164357 -217

npm run start-prod scripting time performance tab

Attempt 1 2 3 4 5 Average
case5, ms 242 237 231 236 234 236
case6, ms 185 186 185 176 180 183
Diff -53

Proposed solution

Is it possible during production build to convert [ngClass] to [class.] and then bundle the code?
As result, we will have a smaller bundle and seems a bit faster application.

Alternatives considered

None.

Activity

  1. added
    area: core
    on Apr 25, 2022
  2. modified the milestone: needsTriage on Apr 25, 2022
  3. added
    area: common
    on Apr 25, 2022
  4. changed the title [-]Convert [ngClass]="..." to [class.x]="..." during prod build[/-] [+]Convert [ngClass]="{x: ...}" to [class.x]="..." during prod build[/+] on Apr 26, 2022
  5. added
    feature
    on May 16, 2022
  6. modified the milestones: needsTriage, Backlog on May 16, 2022
  7. angular-robot commented on May 17, 2022

    @angular-robot
    Contributor

    This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

    You can find more details about the feature request process in our documentation.

  8. angular-robot commented on Jun 26, 2022

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  9. added
    feature: under consideration
    and removed on Jul 15, 2022
  10. alxhub commented on Aug 11, 2022

    @alxhub
    Member

    This seems like a good candidate for an extended diagnostic instead - [ngClass] with a literal could be converted to individual class bindings.

  11. 1 remaining item

  12. added and removed
    feature: under consideration
    on Aug 11, 2022
  13. modified the milestones: Backlog, needsTriage on Aug 11, 2022
  14. added
    P3
    on Nov 16, 2022
  15. modified the milestones: needsTriage, Backlog on Nov 16, 2022
  16. JeanMeche commented on Aug 31, 2025

    @JeanMeche
    Member

    We added a new migration with #62983, it'll be available in v21 this fall.

  17. angular-automatic-lock-bot commented on Oct 1, 2025

    @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.

  18. locked and limited conversation to collaborators on Oct 1, 2025
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: corecompiler: extended diagnosticscore: styling bindings

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions