Change detection and zone.js

Intermediate · 13 min

How Angular has historically known when to re-check the DOM — and why that required patching the browser's own async APIs.

Before thisSignals, computed values, and effects

Joshua Morony · 13 min

Change detection and zone.js

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Joshua MoronyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • zone.js patches the browser's own async APIs — setTimeout, Promise callbacks, event listeners — so Angular finds out whenever any of them run, without you telling it to.
  • Change detection's default behavior, absent any optimization, is to re-check every component in the tree from the root down, on every one of those notifications — not just the component where something happened.
  • This is the actual cost zone.js was solving for, and the actual cost `OnPush` and signals both exist to reduce — understanding it is what makes the next two lessons' optimizations make sense as optimizations, not arbitrary rules.

Knowing when to check is the actual hard problem

A template binding like {{ user.name }} only shows the right value if Angular re-evaluates it after user.name changes. The genuinely hard problem underneath that isn’t how to re-render — it’s when. Your code can change a property from literally anywhere: a button click, a setTimeout callback, a promise resolving, an HTTP response arriving seconds later. Angular has no way to directly observe “a JavaScript variable just changed” — JavaScript simply doesn’t expose that as an event you can subscribe to. So how does Angular know when it might be time to re-check the DOM?

zone.js: patching the browser so Angular gets notified

Historically, Angular’s answer was zone.js — a library that monkey-patches the browser’s own asynchronous APIs. setTimeout, setInterval, DOM event listeners, Promise callbacks, XMLHttpRequest/fetch completions — zone.js wraps all of them, so that whenever any of these async operations actually run their callback, Angular finds out, automatically, with no code of yours doing anything to request it.

This is a genuinely clever workaround for a real limitation. Since Angular can’t watch for arbitrary property mutations directly, it instead watches for the moments mutations are likely to have happened — any point where your code got control back from something asynchronous. A click fires its handler → zone.js tells Angular → Angular runs a change-detection pass. A setTimeout fires → same thing. This is why changing a component’s property inside an ordinary method, with no special Angular API involved, “just works” without you ever manually telling Angular to re-render.

The cost: checking far more than usually necessary

Here’s the part that matters for the next two lessons: by default, when zone.js notifies Angular that something async happened, Angular’s response is to re-check the entire component tree, from the root, regardless of where the triggering event actually occurred. Click a button deep in one corner of a large app, and — absent any optimization — every component everywhere gets its bindings re-evaluated, not just the ones plausibly affected by that click.

That sounds wasteful, and it is, in exactly the way it sounds. It’s also not a mistake — it’s the safe default for a system that, at bottom, can’t actually see which specific data changed, only that something, somewhere might have. Being conservative (check everything) is the price of not needing you to manually wire up what depends on what.

Escaping zone.js deliberately, before signals existed to do it properly

Before signals gave components a precise way to opt out of unnecessary checks, Angular offered a cruder escape hatch for exactly the cases where zone.js’s blanket notifications were clearly wasteful: NgZone.runOutsideAngular(). Wrapping a noisy, high-frequency operation — a requestAnimationFrame loop driving a custom animation, a third-party library firing constant events — inside it tells Angular “don’t run change detection in response to anything that happens in here,” and you call ngZone.run(() => { ... }) explicitly only when something inside that block genuinely needs to update the view. It’s a manual, coarse version of the same instinct OnPush and signals both refine into something more precise and far less error-prone to get right.

This lesson’s video is specifically about whether zone.js is making your app slow, and walks through exactly this full-tree-check behavior with real profiling — watch it with this cost in mind, because the next two lessons are both direct answers to it: OnPush, which lets a component opt out of being checked unless its own inputs have actually changed, and signals, which make “something, somewhere might have changed” precise enough that Angular can eventually stop needing zone.js’s blanket notifications at all.

Check yourself

  1. 01If Angular didn't patch `setTimeout` at all, what would go wrong after `setTimeout(() => { this.name = 'Ada'; }, 1000)` ran inside a component?

    `this.name` would change in memory, but nothing would tell Angular that a re-render might be needed — change detection only runs when Angular is notified something might have changed, and an unpatched `setTimeout`'s callback would run completely outside Angular's awareness. The template would keep showing the old value until something unrelated happened to trigger a re-check.

  2. 02Without OnPush or signals, roughly how much of the component tree does a single button click's change-detection pass check?

    All of it — by default, Angular's change detection walks the entire component tree from the root on every notification zone.js reports, checking every component's bindings whether or not that component could plausibly have been affected by whatever just happened.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark