Modern Angular: Zoneless Change Detection
Zoneless Change Detection: Build apps that run without the overhead of Zone.js for faster runtimes and tiny bundle sizes.
For over a decade, Angular's reactivity depended on Zone.js—a foundational polyfill that monkey-patched native browser APIs (setTimeout, Promise, event listeners, fetch, and XHR) to detect when asynchronous work resolved. Once an asynchronous event finished, Zone.js alerted Angular to dirty-check components across the tree.
While that abstraction made early onboarding painless, it carried distinct architectural costs:
- **Bundle Overhead:** Zone.js adds ~30–40 kB of runtime polyfills to every production build.
- **Coarse-Grained Traversal:** Zone.js only knows that an async event occurred—not where—forcing Angular to traverse broad component subtrees during change detection.
- **Obscured Stack Traces:** Stack traces frequently became unreadable, masked behind layers of internal Zone monkey patches (zone.js/fesm2015/zone.js).
- **Runtime Friction:** Competing Zone instances often caused friction when embedding Angular elements inside micro-frontends, Web Workers, or native Web Components.
- With Angular standardizing on Signals and an internal reactive scheduler, applications can operate completely Zoneless—resulting in faster execution, native async/await performance, cleaner error stacks, and leaner bundles.
## 1. Zone.js vs. Zoneless Angular at a Glance
| Dimension | Zone.js-Based Angular | Modern Zoneless Angular |
| --- | --- | --- |
| Trigger Mechanism | Monkey-patched asynchronous browser APIs | Explicit reactive notifications (Signals, template events, async pipe) |
| Polyfill Footprint | ~33 kB added to polyfills bundle | 0 kB (zone.js eliminated entirely) |
| Async Stack Traces | Deep, monkey-patched Zone calls | Clean, native browser execution stacks |
| Native async/await | Downleveled or intercepted via Zone | Pure native ES2017+ execution |
| Tree Traversal | Broad checks across component branches | Targeted notifications sent straight to dirty views |
| Micro-Frontends & Custom Elements | Complex due to overlapping Zone scopes | Frictionless standard JavaScript integration |
## 2. Bootstrapping a Zoneless Application
- **Adopting Zoneless requires two quick steps:** configuring the root provider in app.config.ts and removing zone.js from the build configuration.
### 2.1 The Provider Configuration (app.config.ts)
Enable the built-in zoneless scheduler via provideExperimentalZonelessChangeDetection():
```ts
// src/app/app.config.ts
import { ApplicationConfig, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
// Replaces provideZoneChangeDetection() completely
provideExperimentalZonelessChangeDetection(),
provideRouter(routes, withComponentInputBinding()),
provideHttpClient(withFetch())
]
};
```
### 2.2 Stripping zone.js from angular.json
To recover bundle savings, remove zone.js from your build polyfills:
```json
// angular.json
{
"projects": {
"my-app": {
"architect": {
"build": {
"builder": "@angular/build:application",
"options": {
"browser": "src/main.ts",
"polyfills": [
// REMOVE "zone.js" FROM THIS ARRAY
]
}
}
}
}
}
}
```
The build pipeline will skip Zone polyfills during compilation, lightening initial page load weight.
## 3. How the Zoneless Scheduler Determines When to Render
Without Zone.js listening to background timers and promises, Angular relies on explicit reactive signals and framework entry points:
```text
Zoneless Triggers
│
┌───────────────────────────┼───────────────────────────┐
▼ ▼ ▼
Signal Mutations Template DOM Events Async Pipe
(signal.set(), update()) ((click), (input)) (obs$ | async)
│ │ │
└───────────────────────────┼───────────────────────────┘
│
▼
[ ChangeDetectionScheduler ]
│
▼
Schedules microtask render pass
Refreshes ONLY targeted dirty views
```
Angular schedules a render pass whenever one of these triggers occurs:
- **Signal Mutations:** Calling .set() or .update() on a signal() or model() bound in the view.
- **Template Event Bindings:** User interactions triggered from template bindings (e.g., (click)="onSave()").
- AsyncPipe (| async): Emitting a new value into an Observable consumed via the async pipe.
- **Input Updates:** When an input() signal or @Input() changes on a child component.
- **Explicit Notifications:** Calling ChangeDetectorRef.markForCheck().
## 4. Writing Zoneless-Compatible Components
Most modern Angular components built with Signals are Zoneless-ready out of the box. However, unmanaged asynchronous state mutations require a quick refactor.
### 4.1 The Reactive Standard: Signals
Signals notify the framework's internal scheduler the exact moment a value changes:
```ts
// src/app/features/zoneless-counter.component.ts
import { Component, signal, computed } from '@angular/core';
@Component({
selector: 'app-zoneless-counter',
standalone: true,
template: `
`
})
export class ZonelessCounterComponent {
count = signal(0);
doubleCount = computed(() => this.count() * 2);
increment(): void {
this.count.update(c => c + 1);
}
incrementDelayed(): void {
// In Zoneless mode, mutating the Signal alerts the scheduler directly—
// no monkey-patched setTimeout needed!
setTimeout(() => {
this.count.update(c => c + 1);
}, 1000);
}
}
```
### 4.2 The Anti-Pattern: Unbound Mutable Class Fields
In legacy Zone-driven code, developers often mutated raw class properties inside asynchronous timers or callbacks:
```ts
// ❌ FAILS IN ZONELESS: Angular never gets notified
export class BrokenZonelessComponent {
rawCounter = 0;
startTimer(): void {
setInterval(() => {
// In Zone.js, monkey-patched setInterval triggered a global check.
// In Zoneless, this property mutates in memory, but the UI never re-renders!
this.rawCounter++;
}, 1000);
}
}
```
The Fix: Wrap the state in a signal() so mutations notify the scheduler:
```ts
// ✅ ZONELESS-READY: Signal notifies the scheduler automatically
export class WorkingZonelessComponent {
reactiveCounter = signal(0);
startTimer(): void {
setInterval(() => {
this.reactiveCounter.update(val => val + 1);
}, 1000);
}
}
```
## 5. Bridging Third-Party Non-Signal Libraries
When integrating external libraries (e.g., Leaflet, Google Maps, Chart.js, or WebRTC handlers) that emit updates outside of Signals or Angular template events, notify the scheduler via ChangeDetectorRef.markForCheck():
```ts
import { Component, ChangeDetectorRef, inject, OnInit } from '@angular/core';
import { ThirdPartySdk } from 'external-sdk';
@Component({
selector: 'app-sdk-bridge',
standalone: true,
template: `
Count: {{ count() }}
Double: {{ doubleCount() }}
Connection Status: {{ status }}
`
})
export class SdkBridgeComponent implements OnInit {
private cdr = inject(ChangeDetectorRef);
status = 'Disconnected';
ngOnInit(): void {
ThirdPartySdk.onEvent((payload: string) => {
this.status = payload;
// Manually trigger the Zoneless scheduler for this view
this.cdr.markForCheck();
});
}
}
```
## 6. Testing Strategies for Zoneless Components
When testing Zoneless components with TestBed, include provideExperimentalZonelessChangeDetection() and use fixture.whenStable() to wait for microtask renders to resolve:
```ts
// zoneless-counter.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { provideExperimentalZonelessChangeDetection } from '@angular/core';
import { ZonelessCounterComponent } from './zoneless-counter.component';
describe('ZonelessCounterComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ZonelessCounterComponent],
providers: [
provideExperimentalZonelessChangeDetection()
]
}).compileComponents();
});
it('should update count reactively upon signal mutation', async () => {
const fixture = TestBed.createComponent(ZonelessCounterComponent);
const component = fixture.componentInstance;
component.increment();
// In zoneless tests, whenStable() resolves after scheduled microtask passes
await fixture.whenStable();
const heading = fixture.nativeElement.querySelector('h2').textContent;
expect(heading).toContain('Count: 1');
});
});
```
Going Zoneless removes runtime overhead, sharpens debugging stacks, and aligns Angular applications directly with standard modern web primitives.