You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Select dropdowns with ngValue binding perform far worse than expected #41330
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.
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.
Reacted by Dmitry A. Efimenko, Arthur Mudrick and Maurizio Cescon
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.
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.
+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 :)
Activity
AndrewKushnir commentedon Mar 23, 2021
Potentially related PR: #25859 and duplicate ticket #19723.
AndrewKushnir commentedon Mar 24, 2021
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 invokewriteValuefunction of theSelectControlValueAccessorclass, 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 unnecessarywriteValuecalls 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
SelectMultipleControlValueAccessorclass 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.
theodorejb commentedon Mar 24, 2021
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
AfterViewCheckedit is no longer necessary to callwriteValue()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.artuska commentedon Apr 6, 2022
+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 :)
fix(forms): improve select performance and ensure that selected optio…
fix(forms): improve select performance
fix(forms): improve select performance
fix(forms): improve select performance
fix(forms): improve select performance
fix(forms): improve select performance (#61949)
angular-automatic-lock-bot commentedon Jul 11, 2025
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.