Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance issue with @angular/material autocomplete

I am using Angular/Material Autocomplete. While loading data to the Autocomplete getting serious performance issues,like the rendering takes around 30 seconds and it takes more than 10 seconds to become stable,data is loaded from the server, and the data received from the server is quite fast. To Over Come that issue i used cdkVirtualScroll, after scrolling down to end and clicking again the text box it's loading empty popup after scroll its loading values.

html

   <mat-form-field>
      <input type="text" placeholder="Pick one" aria-label="Number" matInput [matAutocomplete]="auto">
      <mat-autocomplete #auto="matAutocomplete" (opened)="panelOpened()">
        <cdk-virtual-scroll-viewport itemSize="48" style="height: 240px" minBufferPx="96" maxBufferPx="144">
          <mat-option *cdkVirtualFor="let option of options" [value]="option">
            {{option}}
          </mat-option>
        </cdk-virtual-scroll-viewport>
        </mat-autocomplete>
    </mat-form-field>

TS

export class AppComponent  {
  options: string[] = [];

  @ViewChild(CdkVirtualScrollViewport, {static: true}) viewport: CdkVirtualScrollViewport;

  constructor() {
    for (let i = 0; i < 10000; i++) {
      this.options.push("#"+i);
    }
  }

  panelOpened() {
    if (this.viewport) {
      this.viewport.checkViewportSize();
    }
  }
}

Check the ex: https://stackblitz.com/edit/angular-7vcohv?file=src%2Fapp%2Fapp.component.html

like image 466
Vignesh Avatar asked May 29 '19 05:05

Vignesh


1 Answers

I'm not sure how many options the mat-autocomplete is targeted to support, but my suggestions to improve the performance are:

  1. Fill the autocomplete only after the user typed at least 2 characters.
  2. Implement a server-side search and fill the auto-complete options after you got smaller amount of options.
  3. If you think this is an issue with the mat-autocomplete component, you can open an issue in the @angular/material repository.
like image 85
TheUnreal Avatar answered Oct 13 '22 06:10

TheUnreal