Skip to content

Select dropdowns with ngValue binding perform far worse than expected #41330

Closed

Description

@theodorejb

🐞 bug report

Affected Package

The issue is caused by package @angular/forms

Is this a regression?

Regression from AngularJS.

Description

I noticed that there was a noticeable delay when populating a select dropdown with several hundred+ items using an ngValue binding, which does not occur with other frameworks (I also tested AngularJS and Svelte). Digging into the Angular code it turns out that the _compareWith function in select_control_value_accessor.ts is being called an absurdly high number of times.

I expected that this function would just be called no more than once for each item in the dropdown (i.e. loop through the options until a match is found). However, it is actually called exponentially more. E.g. for a dropdown with 1000 items and the 500th item bound to the ngModel, the function is called 501,001 times. If there are 3000 items in the dropdown and the 1500th item is selected, the _compareWith function is called 1,002,501 times. It gets even worse if an option nearer the end of the list is selected.

🔬 Minimal Reproduction

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

Note that I added a custom compareWith method in this example to log how many times it is called, but the performance is just as bad with the default internal comparison method.

I think the problem stems from Angular's SelectControlValueAccessor.writeValue() method being called over and over for each individual option element that is rendered, rather than once after all the options are rendered.

🌍 Your Environment

Angular Version: 11.2.6

Anything else relevant?
This is especially problematic for our users with slower mobile devices.

Activity

  1. added this to the needsTriage milestone on Mar 23, 2021
  2. added
    area: performance
    on Mar 23, 2021
  3. AndrewKushnir commented on Mar 23, 2021

    @AndrewKushnir
    Contributor

    Potentially related PR: #25859 and duplicate ticket #19723.

  4. added
    P3
    on Mar 23, 2021
  5. modified the milestones: needsTriage, Backlog on Mar 23, 2021
  6. AndrewKushnir commented on Mar 24, 2021

    @AndrewKushnir
    Contributor

    I've performed the necessary investigation and confirmed that the problem is caused by the fact that for each newly added <option> with [ngValue] binding we invoke writeValue function of the SelectControlValueAccessor class, which goes through the set of available options again (see here), which leads to close to quadratic complexity (O(N^2)), which is inefficient. While doing the research I came up with some POC changes that could eliminate unnecessary writeValue calls and it turned out that the solution is very similar to the one proposed in #25859.

    We'll need to perform additional investigation to see whether this change affects any use-cases. I believe the SelectMultipleControlValueAccessor class would also be affected by the same problem.

    We'll perform further investigation and will keep this thread updated, but there is no ETA at this moment.

  7. added
    P4
    and removed
    P3
    on Mar 24, 2021
  8. theodorejb commented on Mar 24, 2021

    @theodorejb
    ContributorAuthor

    Thanks for the investigation @AndrewKushnir. I did some experimentation this morning and found that the issue could be addressed as part of PR #23784 since when using AfterViewChecked it is no longer necessary to call writeValue() over and over for individual option elements (the method is instead called once after all the options are done being added/removed). I confirmed that this approach fixes the issue in our app and updated the PR.

  9. artuska commented on Apr 6, 2022

    @artuska

    +1 for this issue, also thank you for the stackblitz reproduction. I have an empty which populates with 250 countries after a while -- i just wrote console.log('aaa') inside the compareWith function and got 30+ thousands function calls and my browser freezed for a long time :)

  10. angular-automatic-lock-bot commented on Jul 11, 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.

  11. locked and limited conversation to collaborators on Jul 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P4area: formsarea: performancestate: confirmed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions