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/awaitcannot 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,
markForCheckis called (also done by theasyncpipe),- a template or host event listener runs,
- a component is created or destroyed, or a view is attached,
ComponentRef.setInputis used.
Migration path:
- Make components
OnPushcompatible. If a component works correctly withOnPush(state changes through signals, inputs, events or async pipe), it works zoneless. This can be done while zone.js is still present. - Replace mutable fields that change in callbacks with signals. Fields changed in
setTimeoutor a subscription need a signal ormarkForCheck. - Remove reliance on
NgZone.onStableoronMicrotaskEmpty. UseafterNextRenderorafterEveryRender. - Use
provideZonelessChangeDetection(), remove the polyfill, and fix tests (fixture.whenStable()instead ofdetectChangesloops, andTestBed.tick()for effects). - Check libraries. Angular Material and CDK support zoneless. Older libraries may need
markForCheckwrappers. - 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.
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.