Skip to content

Signal forms: poor performances with large arrays #67212

@kanidjar

Description

@kanidjar

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

Formfields provide poor performance with large array as values.

For example, emitting an array of 15000 numbers:

Duration (ngModel): 0.04500007629394531ms
Duration (formField): 38.329999923706055ms

With objects, it can go up to 2000ms!

https://stackblitz.com/edit/formfield-with-large-array?file=src%2Fmain.ts

import { Component, model, signal } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { form, FormValueControl, FormField } from '@angular/forms/signals';

@Component({
  selector: 'app-custom-control',
  template: `
  <button (click)="emitFormfield()">Emit array of 15000 numbers (formField)</button>
  <button (click)="emitNgModel()">Emit array of 15000 numbers (ngModel)</button>
  <br/><br/>
  <div>Duration (ngModel): {{ durationNgModel() }}ms</div>
  <div>Duration (formField): {{ durationFormField() }}ms</div>
`,
})
export class CustomControl implements FormValueControl<number[]> {
  private _numbers: number[] = Array.from({ length: 15000 }, (_, i) => i);

  public value = model<number[]>([]);
  public valueAsNgModel = model<number[]>([]);

  public durationNgModel = signal(0);
  public durationFormField = signal(0);

  public emitNgModel() {
    const startTime = performance.now();

    this.valueAsNgModel.set([...this._numbers]);

    const endTime = performance.now();
    this.durationNgModel.set(endTime - startTime);
  }

  public emitFormfield() {
    const startTime = performance.now();

    this.value.set([...this._numbers]);

    const endTime = performance.now();
    this.durationFormField.set(endTime - startTime);
  }
}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CustomControl, FormField],
  template: `
    <app-custom-control [formField]="myForm.myField" [(valueAsNgModel)]="myModel" />
  `,
})
export class App {
  public myModel = signal([]);
  public myForm = form(
    signal({
      myField: [],
    })
  );
}

bootstrapApplication(App);

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/formfield-with-large-array?file=src%2Fmain.ts

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI       : 21.1.1
Angular           : 21.1.1
Node.js           : 20.19.1
Package Manager   : npm 10.8.2
Operating System  : linux x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/animations       │ 21.1.1            │ ^21.1.1           │
│ @angular/aria             │ 21.1.1            │ ^21.1.1           │
│ @angular/build            │ 21.1.1            │ ^21.1.1           │
│ @angular/cli              │ 21.1.1            │ ^21.1.1           │
│ @angular/common           │ 21.1.1            │ ^21.1.1           │
│ @angular/compiler         │ 21.1.1            │ ^21.1.1           │
│ @angular/compiler-cli     │ 21.1.1            │ ^21.1.1           │
│ @angular/core             │ 21.1.1            │ ^21.1.1           │
│ @angular/forms            │ 21.1.1            │ ^21.1.1           │
│ @angular/platform-browser │ 21.1.1            │ ^21.1.1           │
│ @angular/router           │ 21.1.1            │ ^21.1.1

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Relationships

None yet

Development

No branches or pull requests

Issue actions