question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

select / <option *ngFor [ngValue]> performance

See original GitHub issue

The performance of creating <option *ngFor [ngValue]> is pretty sad (the syntax described at https://angular.io/api/forms/SelectControlValueAccessor)

  <select [formControl]="control">
    <option *ngFor="let value of values" [ngValue]="value">{{ value }}</option>
  </select>

Reproduction: https://stackblitz.com/edit/angular-ivy-zrphum

See console for times. Angular needs ~25ms to render 100 options. And it gets even worse when you add BrowserAnimationsModule (as in the reproduction). It adds ng-star-inserted class to every option and increases the render time quite a bit. Then you are at ~35ms.

As a comparison just innerHTML’ing 100 options needs ~0.5ms. So if i only have string values, creating the options via innerHTML is 70x faster and works just as well with formControl/ngModel.

Btw. enableProdMode() method has no effect anymore since ivy as the value that it sets to true is not used by ivy’s debug mode. It only removes the “not in prod mode” warning 😄 - so I can’t show you the performance on stackblitz without all the debug overhead.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
sodcommented, May 28, 2020

Oh nice. Thank you for having the stamina holding onto that PR for 2 years 😃

3reactions
theodorejbcommented, May 28, 2020

One reason that selects perform poorly (at least when using ngModel) is that Angular sets the select value and selectedIndex over and over for every single option element that is rendered, rather than setting it once after rendering all the options. This would be fixed by PR #23784.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Selected option Angular 4, ngValue don't work - Stack Overflow
Im trying to have a selected option when I receive the object workOrder.supplier, I tryed NgValue, ng-selected, compareWith but dont work, I ...
Read more >
SELECT --> OPTION, using value vs ngValue : r/Angular2
I just recently figured out that there is an alternative for value property on OPTION part of the SELECT, namely ngValue.
Read more >
NgSelectOption - Angular
Tracks simple string values bound to the option element. For objects, use the ngValue input binding. Resources. About ...
Read more >
Using [value] vs [ngValue] in Angular - Upmostly
When binding a dropdown/select list in Angular, you will come across two ... <select [(ngModel)]="selectedBook"> <option *ngFor="let book of ...
Read more >
Angular–CompareWith - The art of simplicity
This directive is especially useful when your select options are dynamically ... <option [ngValue]="option" *ngFor="let option of options">.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found