← Back to Angular, page 8

angular-0143 · Hard senior

How does zone.js make change detection automatic, what are its costs, and how do you migrate an application to zoneless?

zone.js patches browser async APIs so Angular knows when a task finishes and then runs change detection. The costs are extra work for every event and timer, larger bundles, and awkward async await support. Zoneless uses signals, markForCheck and events to schedule updates instead. Migration means making components OnPush compatible, using signals, and removing the zone.js polyfill.

Full answer

How zone.js works: zone.js replaces browser APIs (setTimeout, addEventListener, Promise.then, XMLHttpRequest, and others) with wrapped versions. A zone is an execution context that survives across async calls. NgZone is Angular's zone. When code runs inside it, and its microtask queue becomes empty (onMicrotaskEmpty), Angular runs ApplicationRef.tick(). So any event, timer or HTTP response can update the screen with no extra code.

Costs:

  • Every patched event listener and timer triggers a full pass, even when no state changed (a mouse move listener, a polling timer).
  • Bundle size and start-up work for the patches.
  • Native async/await cannot be patched, so the CLI down-levels async functions to promises. This also blocks some modern features.
  • Third-party libraries run inside the zone and trigger passes unexpectedly, unless wrapped in runOutsideAngular.
  • Stack traces and debugging are harder.

Zoneless: provideZonelessChangeDetection() (stable since Angular 20.2), and remove zone.js from polyfills. Angular then schedules a pass when:

  • a signal read by a template changes,
  • markForCheck is called (also done by the async pipe),
  • a template or host event listener runs,
  • a component is created or destroyed, or a view is attached,
  • ComponentRef.setInput is used.

Migration path:

  1. Make components OnPush compatible. If a component works correctly with OnPush (state changes through signals, inputs, events or async pipe), it works zoneless. This can be done while zone.js is still present.
  2. Replace mutable fields that change in callbacks with signals. Fields changed in setTimeout or a subscription need a signal or markForCheck.
  3. Remove reliance on NgZone.onStable or onMicrotaskEmpty. Use afterNextRender or afterEveryRender.
  4. Use provideZonelessChangeDetection(), remove the polyfill, and fix tests (fixture.whenStable() instead of detectChanges loops, and TestBed.tick() for effects).
  5. Check libraries. Angular Material and CDK support zoneless. Older libraries may need markForCheck wrappers.
  6. Enable provideBrowserGlobalErrorListeners() because zone.js no longer reports unhandled errors for you.

Benefits: smaller bundle, fewer passes, simpler async stack traces, better interoperability with micro-frontends. Risk: silent staleness where data changes but no notification happens. The cure is signals for all UI state.

typescript
import { ApplicationConfig, ChangeDetectionStrategy, Component, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection, signal } from '@angular/core';

export const appConfig: ApplicationConfig = {
  providers: [provideZonelessChangeDetection(), provideBrowserGlobalErrorListeners()],
};

@Component({
  selector: 'app-clock',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<p>Seconds since open: {{ seconds() }}</p>`,
})
export class ClockComponent {
  readonly seconds = signal(0);

  constructor() {
    // No zone patch is needed. The signal write notifies the scheduler.
    setInterval(() => this.seconds.update((s) => s + 1), 1000);
  }
}

// angular.json: remove "zone.js" from the "polyfills" array of build and test targets.

Angular 20.3, type-checked. The sample shows the zoneless bootstrap and a component that updates through a signal inside a timer. It was not run in a browser here.

Key points

  • zone.js patches async APIs so Angular runs a pass after every task.
  • Costs: extra passes, bundle size, async/await down-leveling, library surprises.
  • Zoneless schedules passes from signals, `markForCheck`, events and view changes.
  • Migrate by making components OnPush compatible and using signals, then remove the polyfill.

Common mistakes

  • Removing zone.js while components still mutate plain fields in callbacks.
  • Relying on `NgZone.onStable` in zoneless mode.
  • Forgetting global error listeners after removing zone.js.

Follow-ups

  • What triggers a pass in zoneless mode?
  • How do tests change with zoneless?
  • Why can code run outside the zone and still update the UI under zoneless?

Tips

  • Describe zone.js as patching, then list costs.
  • Present the migration as OnPush readiness first.
  • Mention error listeners and tests.

Evaluation rubric

  • Explains zone.js mechanics and costs.
  • Lists zoneless triggers correctly.
  • Gives a safe migration plan.