← Back to Angular, page 10

angular-0188 · Hard senior

A form with hundreds of controls (a long admission form or a spreadsheet-like grid) is slow to type in. How do you find the cause and speed it up?

Typing is slow when each key triggers validation, valueChanges subscribers and change detection for the whole form. Profile first, then reduce work: updateOn blur for heavy controls, OnPush sub-form components, debounced and async validation, fewer valueChanges subscriptions, track in lists, virtual scrolling for many rows, and splitting the form into steps.

Full answer

Start by measuring. Use the browser Performance panel while typing in a field, and the Angular DevTools profiler. Look for long tasks per keystroke, and check which components are checked and which functions are slow (validators, template methods, valueChanges subscribers). Without that, you may optimise the wrong thing.

Typical causes and fixes:

  1. Every change triggers heavy work. By default, a control updates on each input event: it runs validators, updates its parent group (which runs the group validator and recalculates validity up to the root), and emits valueChanges and statusChanges. With hundreds of controls, a group validator or a root valueChanges subscriber that loops over everything is expensive. Fixes: updateOn: 'blur' (or 'submit') for the form, group or specific controls, so work happens once when the user leaves a field. Debounce expensive reactions with debounceTime. Avoid subscribing to form.valueChanges for the whole form, and subscribe to the control that matters.
  2. Validators are slow or call servers. Keep synchronous validators cheap. Async validators should be debounced (timer in the validator) and run only if the sync validators pass.
  3. Change detection checks everything. Split the form into sub-form components, each with OnPush, and pass the child FormGroup down. With ControlContainer (viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }]) sub-components can use formControlName inside the parent form. A keystroke in one section then only checks that section.
  4. Template methods and getters. A method like getError('email') that calls form.get(...) many times per check adds up. Use direct control references (form.controls.email), @let, or computed values, and show errors only for touched controls.
  5. Many rows. In a FormArray of hundreds of rows, use track on a stable id, virtual scrolling for the rows that are visible, or pagination. Create row groups lazily.
  6. Too much on the page. Split a long form into steps or accordions rendered on demand (@defer, @if). Autosaving the form can use a debounced write, not an immediate write per key.
  7. Template-driven forms with ngModel on hundreds of inputs are slower than reactive forms because each control registers asynchronously. Prefer reactive forms for large screens.

After each change, measure again with the same typing test. Keep the gains with a small performance test or a budget in review.

typescript
import { Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';

// Captured in Karma (Angular 20.3): three input events, then one blur
//   default controls:     valueChanges emissions while typing = 3, after blur = 3
//   updateOn 'blur':      valueChanges emissions while typing = 0, after blur = 1
export const counts = { change: 0, blur: 0 };

@Component({
  selector: 'app-update-on-demo',
  imports: [ReactiveFormsModule],
  template: `
    <input id="a" aria-label="Default" [formControl]="onChange" />
    <input id="b" aria-label="On blur" [formControl]="onBlur" />
  `,
})
export class UpdateOnDemoComponent {
  readonly onChange = new FormControl('', { nonNullable: true });
  readonly onBlur = new FormControl('', { nonNullable: true, updateOn: 'blur' });

  constructor() {
    this.onChange.valueChanges.subscribe(() => counts.change++);
    this.onBlur.valueChanges.subscribe(() => counts.blur++);
  }
}

Angular 20.3, strict template check passes. A Karma test sends three input events to a field and counts valueChanges emissions for the default setting and for `updateOn: 'blur'`: 3 and 0 while typing, and 1 after blur (the captured numbers are in the comment).

Key points

  • Profile typing first. Look for validators, `valueChanges` subscribers and wide change detection.
  • Reduce work per keystroke with `updateOn: 'blur'`, debounce, and narrow subscriptions.
  • Split into `OnPush` sub-form components using `ControlContainer`. Use `track`, virtual scroll or steps for big lists.
  • Prefer direct control references over template methods, and reactive over template-driven forms for big forms.

Common mistakes

  • Subscribing to the whole form's `valueChanges` to do heavy work.
  • Optimising without a profile.
  • Rendering all rows of a huge `FormArray` at once.

Follow-ups

  • What does `updateOn: 'blur'` change for validation messages?
  • How does `ControlContainer` let a child component join the parent form?
  • When would you split the form into steps?

Tips

  • Start with measuring.
  • Group the causes: work per key, check scope, and size.
  • Mention a measurable test after the fix.

Evaluation rubric

  • Finds causes through profiling.
  • Gives targeted fixes for validation, change detection and size.
  • Verifies with measurement.