Angular performance
Measure what OnPush, signals, zoneless change detection, @defer, incremental hydration and NgOptimizedImage change on a real Angular shop.
- 01Count what change detection checksSee why Eager components check every template, and what OnPush skips, in templates run and in time.
- 02Tell Angular exactly what changedUse signals so only the readers refresh, and run an app without zone.js.
- 03Send and wake parts of a page laterChoose between @defer, incremental hydration with event replay, and NgOptimizedImage for the photo at the top.
Meera's Angular shop and one click
Module 36 looks at this course's ideas through each framework's own tools. Lesson 36.1 did React, and lesson 36.2 did Next.js. This lesson does Angular.
Ravi's friend Meera runs the Angular version of his shop. Her code is a few years old. She has just moved it to Angular 22 with ng update, Angular's update command. Nothing broke, and every page still works.
Asha opens the reviews page from lesson 36.1. It has the cart badge at the top, Add to cart, a box to filter, and 1,000 reviews. She clicks Add to cart. The badge goes from Cart (0) to Cart (1). One number changed. How much work did Angular do to change it?
The course built the page three ways and counted. Here are real frames from Asha's screen, just before her click and just after it:
The OnPush page answered fastest, but its badge never changed. Here is what each page did for that one click:
// Asha's one click on Add to cart, on each reviews page.// checked: component templates Angular ran. ms: from the click to the next paint.const pages = [ { page: "Eager, zone.js", checked: 7006, ms: 46.9, badge: "Cart (1)" }, { page: "OnPush, zone.js", checked: 2, ms: 15.5, badge: "Cart (0)" }, { page: "Signals, no zone.js", checked: 3, ms: 23.1, badge: "Cart (1)" },];for (const { page, checked, ms, badge } of pages) { console.log(`${page}: ${checked} checked in ${ms} ms, then ${badge}`);}- Lines 1 and 2 are comments. A component is one piece of an Angular page, like the badge or one review. Its template is the HTML it draws. A millisecond (ms) is a thousandth of a second.
- Lines 4 to 6 hold each page: the templates Angular ran, the time until Asha saw the next frame, and the badge after.
- Line 9 prints
Eager, zone.js: 7006 checked in 46.9 ms, then Cart (1). ThenOnPush, zone.js: 2 checked in 15.5 ms, then Cart (0). Last,Signals, no zone.js: 3 checked in 23.1 ms, then Cart (1).
So, as the update left it, Angular ran 7,006 templates to change one number. With signals, it ran 3. All three clicks were under the 200 ms that counts as a good INP (lesson 26.1). But the Eager click took twice as long as the signals click. Why it matters: Eager checks grow with the page, and every click pays for them.
A pupil changes one answer and hands in her book. The teacher doesn't know which book changed. So she marks all thirty books again. She compares each mark with the old one. In the end she changes one mark, but she spent the whole break looking.
- In real life: One pupil changes one answer
- In Angular: Asha clicks Add to cart
- In real life: Marking all thirty books again
- In Angular: Running every component's template again
- In real life: Comparing each mark with the old one
- In Angular: Comparing each value with the last one
- In real life: Changing only the marks that differ
- In Angular: Changing only the page parts that differ
Angular is fast when it checks only what changed, sends only the code a visitor needs now, and wakes the rest when someone needs it. OnPush and signals cut the checking. @defer and incremental hydration cut the code. NgOptimizedImage helps the photo at the top.
How the course recorded it
- Meera's pages: Angular 22.2.6, built with
ng build, the production build. Angular's other tools are pinned too. - Two pages: the reviews page, three ways, drawn in the browser. And lesson 35.3's Mint tea page, six ways, drawn on the server by the Node server that
ng buildwrites. - Asha's phone: Chrome 157, with the CPU 4 times slower. The course's own server played a Fast 4G line: 165 ms before each answer, then 8.1 megabits a second. It also compressed text files, as most hosts do.
- Real input: mouse clicks and key presses sent through Chrome's DevTools protocol. Chrome passes both straight to the page. The scroll to the reviews was done by a script, with
scrollIntoView. - Counting: each reviews page was built a second time, with a small counter wrapped around every component's template. The counts were the same in every visit.
- 5 visits: each page and way, taking turns. The lesson quotes the middle one.
How change detection checks a page
When you click, Angular runs your code. Your code may change a value. But Angular doesn't know which values changed. So it looks. That looking is called change detection.
Angular's docs describe it simply. Angular walks your components from top to bottom. For each one, it runs the template's code again and compares each value with the last one. Where a value differs, it changes that part of the page. We'll call one run of a template a check.
Who tells Angular to look? In Meera's app, a library called zone.js. It wraps the browser's clicks, keys and timers. When one of them finishes, it tells Angular, and Angular checks.
Here is one review, as the update left it:
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';import { marked } from 'marked';import { type ReviewData } from './reviews';import { Stars } from './stars'; @Component({ selector: 'app-review', changeDetection: ChangeDetectionStrategy.Eager, imports: [Stars], template: ` <h3 class="review-name">{{ review.name }}</h3> <p class="review-date">{{ review.date }}</p> <app-stars [count]="review.stars" /> <div class="review-text" [innerHTML]="toHtml(review.text)"></div> <button class="helpful" type="button" [attr.aria-pressed]="voted" (click)="voted = !voted">Helpful</button> `,})export class Review { @Input({ required: true }) review!: ReviewData; voted = false; toHtml(text: string) { return marked.parse(text) as string; }}- Lines 6 to 17 describe the component.
@Componenttells Angular its name, its settings and its template. - Line 8 is the line
ng updateadded:ChangeDetectionStrategy.Eager. An Eager component is checked every time Angular's check reaches it. - Lines 11 to 15 are the template. Each
{{ }}and each[ ]is a value Angular compares. - Line 14 fills the review's text. It calls
toHtml, which runs marked, the library that turns Markdown into HTML. - Lines 22 to 24 are toHtml. It does real work on every call.
Now look at what the course counted for Asha's click:
// What the eager page's counting build counted for the click.const checked = { App: 1, Header: 1, CartBadge: 1, AddButton: 1, FilterBox: 1, ReviewList: 1, Review: 1000, Stars: 1000, Star: 5000 };const total = Object.values(checked).reduce((sum, n) => sum + n, 0);console.log(`${total} templates ran for one click.`);console.log(`toHtml ran ${checked.Review} times, once in each Review.`);- Line 2 holds the counts, one per component.
- Line 3 adds them up.
- Line 4 prints
7006 templates ran for one click.Line 5 printstoHtml ran 1000 times, once in each Review.
Every review was checked, and so was every star in it. And since line 14 calls toHtml, Markdown was turned into HTML 1,000 times. Each result was the same as before, so Angular changed nothing in the reviews. Angular's docs warn about this. A slow function in a template runs on every check, and slows the whole check down.
One more detail from the recording. Asha's click took 46.9 ms, but its handlers took almost no time: 0 ms. Angular didn't check inside the click. It waited for whichever came first, a timer or the next frame (callback_scheduler.ts, lines 37 to 62). So the INP parts from lesson 26.2 put the checking in the presentation delay: 45.1 ms. Why it matters: in an Angular trace, look for the checking just before the frame, not inside the click.
OnPush: skip what didn't change
A wiser teacher marks only the books handed in again, and the books of pupils who raise a hand. The rest stay on the shelf. Marking is quick now. But if a pupil writes in a book on the shelf, the teacher never sees it.
- In real life: A book handed in with new pages
- In Angular: A component given a new input value
- In real life: A pupil who raises a hand
- In Angular: An event inside the component
- In real life: A book left on the shelf
- In Angular: A component Angular skips
- In real life: An answer changed in a book left on the shelf
- In Angular: A plain field changed somewhere else
That's OnPush. The docs list when Angular checks an OnPush component:
- It gets a new input value. An input is a value its parent passes in.
- An event happens inside it, like a click, or inside a component it draws.
- Someone marks it for a check, with
markForCheck. - A signal its template reads changes. The next section explains signals.
Since Angular 22, OnPush is the default. A component with no changeDetection line is OnPush (definition.ts, line 352). That's why ng update added the Eager line to every old component: to keep them working the old way. So Meera deleted those lines. Here is the cart badge after that:
@Component({ selector: 'app-cart-badge', template: `<a id="cart" href="#">Cart ({{ cart.count }})</a>`,})export class CartBadge { protected readonly cart = inject(Cart);}- Line 3 is the template. It shows the cart's count. There's no changeDetection line, so the badge is OnPush.
- Line 6 gets the Cart, a shared object that holds the count.
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' })export class Cart { count = 0; add() { this.count++; }}- Line 5 is the count, a plain number.
- Line 8 adds one to it. Add to cart calls this.
The click ran 2 templates: App and AddButton. The click happened inside AddButton, and App draws AddButton. The badge got no new input, and nothing happened inside it. So Angular skipped it, and it kept saying Cart (0). The count was 1. Only the badge was wrong.
Typing changed less than the click. Here is Asha's first key, h, on each page:
// Asha's first key, h, on each page. Times in ms.// drawing: style, layout and paint, the work every page does.const pages = [ { page: "Eager", checked: 6306, ms: 82.5, drawing: 42.2 }, { page: "OnPush", checked: 3, ms: 66.1, drawing: 40.5 }, { page: "Signals", checked: 3, ms: 64.2, drawing: 39.1 },];for (const { page, checked, ms, drawing } of pages) { console.log(`${page}: ${checked} checked, ${ms} ms, ${drawing} ms of drawing`);}- Lines 4 to 6 hold each page: templates run, the key's time, and the time Chrome spent drawing.
- Line 9 prints
Eager: 6306 checked, 82.5 ms, 42.2 ms of drawing. ThenOnPush: 3 checked, 66.1 ms, 40.5 ms of drawingandSignals: 3 checked, 64.2 ms, 39.1 ms of drawing.
The h took 100 reviews off the list, and left 900. Every page had to do that, then work out styles, lay out and paint. That drawing took about 40 ms on all three. The Eager page took 16.4 ms longer than the OnPush page. Why it matters: OnPush removes Angular's own work. The browser's work stays, so measure before you expect a big change.
Pick a page to see what Angular checked for each of Asha's inputs:
- Click: templates run7,006 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 1,000, Stars 1,000, Star 5,000)
- Click: to the next paint46.9 ms
- Key h: templates run6,306 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 900, Stars 900, Star 4,500)
- Key h: to the next paint82.5 ms
- Key o: templates run2,456 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 350, Stars 350, Star 1,750)
- Key o: to the next paint76.2 ms
- Key n: templates run1,756 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 250, Stars 250, Star 1,250)
- Key n: to the next paint35.1 ms
- Key e: templates run1,756 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 250, Stars 250, Star 1,250)
- Key e: to the next paint19.2 ms
- Key y: templates run1,406 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 200, Stars 200, Star 1,000)
- Key y: to the next paint27.5 ms
- The badge after the clickCart (1)
- zone.js, compressed12.8 kB
Pick a page. Each input has two rows: the component templates Angular ran for it, by component, and how long it took until the next paint.
Signals and zoneless: tell Angular what changed
A signal is a value that knows who reads it. You read it by calling it, like count(). When a template reads a signal, Angular notes that. When the signal changes, Angular checks that template on its next pass. Here is the cart with a signal:
import { Injectable, signal } from '@angular/core'; @Injectable({ providedIn: 'root' })export class Cart { readonly count = signal(0); add() { this.count.update((count) => count + 1); }}- Line 5 makes
counta signal that starts at 0. - Line 8 changes it:
updatetakes the old value and returns the new one. - The badge's template now reads it with the call brackets:
template: `<a id="cart" href="#">Cart ({{ cart.count() }})</a>`,
The signals page ran 3 templates for the click: App, AddButton and CartBadge. The badge said Cart (1). Here is how Angular found those three. The click's listener marked AddButton and every component above it (mark_view_dirty.ts, lines 41 to 50). The signal marked CartBadge to refresh. It marked the components above it only to be passed through (view_utils.ts, lines 264 to 280). Then the check refreshed only the marked ones (change_detection.ts, lines 523 to 543).
Now the pupil puts a sticky note on the page she changed. The teacher goes straight to that page. She doesn't need the old school bell, which rang whenever anyone moved. The notes tell her where to look.
- In real life: A sticky note on the page that changed
- In Angular: A signal that changed
- In real life: Going straight to that page
- In Angular: Checking only the templates that read it
- In real life: A bell that rings whenever anyone moves
- In Angular: zone.js, after every click, key and timer
- In real life: No bell, only notes
- In Angular: A zoneless app
That bell is zone.js. The docs say it doesn't know whether anything really changed, so Angular checks more often than it needs to. It also adds code to download and start.
Since Angular 21, apps run without it by default. They are called zoneless. Angular starts every app zoneless (create_application.ts, line 67). An app keeps zone.js only if it asks:
import { provideZoneChangeDetection } from '@angular/core';import { bootstrapApplication } from '@angular/platform-browser';import { App } from './app/app'; bootstrapApplication(App, { providers: [provideZoneChangeDetection({ eventCoalescing: true })],});- Line 6 asks for zone.js with
provideZoneChangeDetection. The build also loads zone.js itself, frompolyfillsin angular.json.
import { bootstrapApplication } from '@angular/platform-browser';
import { App } from './app/app';
bootstrapApplication(App);- Line 4 starts the app with no such line. Meera also removed zone.js from polyfills.
That took zone.js out of the build: 35.9 kB, or 12.8 kB compressed. Zoneless apps need signals, inputs, events or markForCheck to tell Angular when to look. The docs say OnPush isn't required, but it's a good step towards that.
Did signals make typing faster than OnPush? Look at the key code above: no. Both pages checked 3 templates per key. Why it matters: signals made the badge right with the fewest checks, and let zone.js go. The reviews were already skipped by OnPush.
Seeing the checks in DevTools
Angular can draw its checks in Chrome's Performance panel, in a track of its own called Angular. Call enableProfiling() before your app starts, or ng.enableProfiling() in the console. Then record a profile. The docs say it works only in development mode, not in a production build. Each component and each template run gets its own entry. Exercise 5 opens one of the course's traces.
@defer: the reviews' code, later
Now Asha opens the Mint tea page from lesson 35.3. Meera's server draws it, and sends finished HTML. It has the photo, the size picker, Add to cart, and 200 reviews far down the page. The browser then downloads the page's JavaScript. Angular runs it and attaches the click handlers to the HTML it got. That's hydration.
The reviews need marked and their own code. Until now, that code sat in main.js, the file every visit downloads first. An @defer block moves it out:
@defer (on viewport) { <app-reviews />} @placeholder { <section id="reviews"><h2>Reviews</h2><p class="waiting">Loading reviews…</p></section>}- Line 1 starts the block.
on viewportis its trigger: load when the block scrolls into view. - Line 2 is what the block shows once its code has loaded.
app-reviewsis the reviews component. - Lines 3 and 4 are the placeholder, shown until then.
Angular's build puts what the block uses in a file of its own, like lesson 22.4's split files. The docs list other triggers: on idle, the default, plus on interaction, on hover, on immediate, on timer and when. A prefetch trigger can download the file early, before it shows.
// The Mint tea page, watched. Bytes as sent, compressed. Times in ms.const image = { jsBeforeScroll: 80025 };const defer = { jsBeforeScroll: 64302, scrolledAt: 3242.4, reviewsCode: 18518, askedAt: 3247.5 };console.log(`JavaScript before the scroll: ${image.jsBeforeScroll}, then ${defer.jsBeforeScroll}`);console.log(`The reviews' code: ${defer.reviewsCode} bytes, asked for ${(defer.askedAt - defer.scrolledAt).toFixed(1)} ms after the scroll`);- Line 2 holds the NgOptimizedImage page, the way before @defer. Line 3 holds the @defer page.
- Line 4 prints
JavaScript before the scroll: 80025, then 64302. - Line 5 prints
The reviews' code: 18518 bytes, asked for 5.1 ms after the scroll.
So the first visit downloaded 15.7 kB less. But there's a catch. On the server, the docs say, an @defer block draws only its placeholder. So the first HTML held no reviews. Here is Asha's screen right after she scrolled to them:
On the @defer page, she saw "Loading reviews…". The reviews showed 433.7 ms after the scroll, and pushed the footer down. That jump scored 0.175 of CLS, more than the 0.1 that counts as good (lesson 30.1). And a search engine reading the HTML finds no reviews. Why it matters: the docs say not to defer what's on screen at first. For text that must be in the HTML, read the next section.
A café hands you the main menu as you sit down. The dessert menu comes only when you finish your main course. Most people never need it early. But if you wanted to read it first, you'd see only a card saying it's coming.
- In real life: The main menu, handed over at once
- In Angular: main.js, downloaded with the page
- In real life: The dessert menu
- In Angular: The @defer block's own file
- In real life: Finishing your main course
- In Angular: The trigger, like on viewport
- In real life: A card saying "Dessert menu coming"
- In Angular: The @placeholder
Pick a way to see all its numbers. The next two sections explain the last three ways and the photo.
- First byte of the HTML383 ms
- Reviews in the first HTMLyes, all 200
- JavaScript before the scroll77.7 kB
- Page hydrated989.1 ms
- LCP, the photo1,040 ms
- Reviews' code after the scrollnone
- Layout shift after the scroll0
- Helpful pressed, after the click6.4 ms
- Early click on Add to cartlost: the badge still said Cart (0)
Pick a way. Each is one change from the way before it. In one visit, Asha watched, scrolled to the reviews, then clicked Helpful. In the other, she clicked Add to cart as soon as it showed.
Incremental hydration and event replay
Incremental hydration gives you both. The server draws the block's real content into the HTML. The browser still downloads its code later, and hydrates it then. You choose when with a hydrate trigger:
@defer (hydrate on viewport) { <app-reviews />} @placeholder { <section id="reviews"><h2>Reviews</h2><p class="waiting">Loading reviews…</p></section>}- Line 1 is the only change.
hydrate on viewportsays: draw the reviews on the server, and hydrate them when they scroll into view. - Lines 3 and 4 are still the placeholder. Angular uses it only when the browser draws the page itself, like after a click on a link inside the app.
The docs list the same triggers as before, with hydrate in front, plus hydrate never. The last way of the page used hydrate on interaction: hydrate the reviews when someone clicks or types in them.
// After the scroll. codeAt: when the reviews' code arrived, in ms.const pages = [ { page: "on viewport", inHtml: false, codeAt: 3440.3, cls: 0.175 }, { page: "hydrate on viewport", inHtml: true, codeAt: 3578.5, cls: 0 }, { page: "hydrate on interaction", inHtml: true, codeAt: 6093.7, cls: 0 },];for (const { page, inHtml, codeAt, cls } of pages) { console.log(`${page}: in the HTML ${inHtml}, code at ${codeAt}, CLS ${cls}`);}- Lines 3 to 5 hold each way: are the reviews in the first HTML, when their code arrived, and how much the page moved after the scroll.
- Line 8 prints
on viewport: in the HTML false, code at 3440.3, CLS 0.175. Thenhydrate on viewport: in the HTML true, code at 3578.5, CLS 0. Last,hydrate on interaction: in the HTML true, code at 6093.7, CLS 0.
With hydrate on viewport, the reviews were in the HTML, so nothing moved when their code arrived. The right-hand frame in the picture above shows it. The page's first hydration also got shorter, because the reviews waited. It ended at 746.5 ms, against 973.4 ms when the reviews were in main.js.
With hydrate on interaction, scrolling loaded nothing. The code came only when Asha clicked Helpful on the first review. So that first click had to wait for the download and the hydration. Exercise 3 asks how long. Why it matters: on interaction saves the most when few people click, but the first click pays.
Event replay: a click before hydration
What about a click that comes before hydration? Asha clicked Add to cart as soon as she saw it, while the page was still hydrating. The HTML was there, but no handler yet. Event replay keeps such clicks and runs them once hydration is done. In Angular 22, provideClientHydration() turns on incremental hydration (hydration.ts, lines 289 to 291), and that turns on event replay (api.ts, lines 343 to 345). But ng update kept Meera's old setting:
import { ApplicationConfig } from '@angular/core';import { provideClientHydration, withNoIncrementalHydration } from '@angular/platform-browser'; export const appConfig: ApplicationConfig = { providers: [provideClientHydration(withNoIncrementalHydration())],};- Line 2 imports two functions from Angular.
- Line 5 turns on hydration, but
withNoIncrementalHydration()turns incremental hydration off, and event replay with it. The default way is justprovideClientHydration().
// One click on Add to cart, as soon as it showed. Times in ms.// ready: when Angular had hydrated the page.const pages = [ { page: "Updated", clicked: 437.1, ready: 890.9, cart: "Cart (0)" }, { page: "Default", clicked: 418.9, ready: 883.7, cart: "Cart (1)" }, { page: "Hydrate on viewport", clicked: 425, ready: 643, cart: "Cart (1)" },];for (const { page, clicked, ready, cart } of pages) { console.log(`${page}: clicked at ${clicked}, ready at ${ready}, then ${cart}`);}- Lines 4 to 6 hold each way: when Asha clicked, when Angular had hydrated the page, and the badge after.
- Line 9 prints
Updated: clicked at 437.1, ready at 890.9, then Cart (0). ThenDefault: clicked at 418.9, ready at 883.7, then Cart (1). Last,Hydrate on viewport: clicked at 425, ready at 643, then Cart (1).
On the updated page, the click was lost in 5 of 5 visits. On the default page, Angular ran it after hydration, and the badge said Cart (1) at 903.9 ms. The server's HTML had a small script for this. It listened for clicks before Angular arrived. With hydrate on viewport, the page hydrated sooner, so the kept click ran sooner too. Why it matters: an early click should never be lost. Remove withNoIncrementalHydration() once your page works with incremental hydration.
A shop shows all its goods in the window, so you see them at once. An assistant comes to a shelf only when you reach it. If you knock before anyone is there, you write your order on a notepad by the door. The assistant reads it on arrival, so no order is lost.
- In real life: Goods on show in the window
- In Angular: The server's HTML, reviews and all
- In real life: The assistant who comes when you reach a shelf
- In Angular: A hydrate trigger loading and hydrating a block
- In real life: The notepad by the door
- In Angular: Event replay's small script
- In real life: Reading the notepad on arrival
- In Angular: Running the kept clicks after hydration
NgOptimizedImage: the photo first
The last change is the photo at the top. It's the page's biggest element, so it decides LCP, Largest Contentful Paint (lesson 17.1). NgOptimizedImage is Angular's image directive: you write ngSrc instead of src.
<img class="hero" src="mint-tea.jpg" width="1600" height="1067" alt="A cup of mint tea" /><img class="hero" ngSrc="mint-tea.jpg" width="1600" height="1067" priority alt="A cup of mint tea" />- Line 1 is the plain img.
- Line 2 uses
ngSrc. Width and height are required, so the browser keeps the photo's space.prioritymarks it as the photo to load first.
Here is what Meera's server sent for line 2:
<link as="image" href="mint-tea.jpg" rel="preload" fetchpriority="high">
<img ngsrc="mint-tea.jpg" width="1600" height="1067" priority="" alt="A cup of mint tea" class="hero" loading="eager" fetchpriority="high" decoding="sync" ng-img="true" src="mint-tea.jpg">- Line 1 is a preload link, added to the head on the server only (ng_optimized_image.ts, lines 526 to 534). Lesson 17.3 showed what preload does.
- Line 2 has three new attributes:
loading="eager",fetchpriority="high"anddecoding="sync"(lines 498 to 500).
// The photo on two watched pages. Times in ms.const plain = { priority: "Medium, then High", asked: 389.1, arrived: 739.4, lcp: 1020 };const optimized = { priority: "High", asked: 387.2, arrived: 741.9, lcp: 1024 };for (const [name, photo] of Object.entries({ plain, optimized })) { console.log(`${name}: ${photo.priority}, asked at ${photo.asked}, arrived at ${photo.arrived}, LCP ${photo.lcp}`);}- Line 2 is the default page, with the plain img. Line 3 is the same page with NgOptimizedImage. Nothing else differs.
- Line 5 prints
plain: Medium, then High, asked at 389.1, arrived at 739.4, LCP 1020. Thenoptimized: High, asked at 387.2, arrived at 741.9, LCP 1024.
Priority did its job. Chrome asked for the photo at High priority from the start, not Medium. But it asked at the same moment, because the img was in the HTML's first bytes either way. Both photos were in by about 740 ms. Yet LCP came after 1,000 ms on both pages.
Why so late? From 728.3 ms, the main thread was busy starting Angular and hydrating, for 259 ms. Chrome drew a frame at 584.9 ms, then none until 1,020.6 ms. A photo can't show before a frame is drawn. That's the render delay of lesson 17.2. Priority boarding gets you on the plane first, but the plane still can't leave before the doors close. With hydrate on viewport, hydration's long task was much shorter, and LCP was 784 ms.
NgOptimizedImage also makes every image without priority lazy. With an image loader, a function that builds the file's address for an image service, it writes a srcset of sizes too, like lesson 18.2. The docs cover sizes, ngSrcset, and fill for photos that fill a box. It added 2.4 kB to main.js, compressed. Why it matters: mark the LCP photo with priority, then check what else keeps the main thread busy.
Which Angular tool fixes it?
Read this table across. Each row is one way of the Mint tea page, from its middle visits.
| Way | JavaScript before the scroll | LCP | Reviews' code | Shift after the scroll | Early click |
|---|---|---|---|---|---|
| As the update left it | 77.7 kB | 1,040 ms | In main.js, at load | 0 | Lost |
| Angular 22's hydration default | 77.7 kB | 1,020 ms | In main.js, at load | 0 | Kept |
| NgOptimizedImage with priority | 80.0 kB | 1,024 ms | In main.js, at load | 0 | Kept |
| @defer (on viewport) | 64.3 kB | 620 ms | 18.5 kB, after the scroll | 0.175 | Kept |
| @defer (hydrate on viewport) | 66.6 kB | 784 ms | 18.5 kB, after the scroll | 0 | Kept |
| @defer (hydrate on interaction) | 66.6 kB | 780 ms | 18.5 kB, after the Helpful click | 0 | Kept |
Three patterns stand out:
- Code: moving the reviews out of
main.jscut what every visit downloaded, and shortened hydration. - The first HTML: only the hydrate triggers kept the reviews in it, and kept the page still.
- Clicks: only the way the update left it lost the early click.
Now sort these problems by the Angular tool that fixes them:
- 1,000 review templates run again for one click
- A cart badge that stays at 0 on an OnPush page
- zone.js starting a check after every click, key and timer
- A Markdown library in main.js, used only far down the page
- A widget below the fold that most visitors never open
- Deferred reviews that leave only a placeholder in the first HTML
- An early click lost before the page hydrates
- A hero photo asked for at Medium priority
- Photos far down the page downloaded with the first files
- A photo with no width and height that pushes the text down
Sort each problem by the Angular tool that fixes it.
Common mix-ups
- "OnPush is free speed." No. It skipped more than 7,000 checks, but the badge stopped counting. A value that changes outside a component needs an input or a signal to reach it.
- "Signals make every key faster than OnPush." No. Both pages checked 3 templates per key. Signals fixed the badge and let zone.js go.
- "An @defer block is in the server's HTML." No. The server draws only its placeholder. The reviews arrived after the scroll, and the page jumped.
- "Incremental hydration needs withIncrementalHydration()." No. In Angular 22,
provideClientHydration()turns it on, and event replay with it. - "Zoneless needs provideZonelessChangeDetection()." No. Since Angular 21, apps are zoneless unless they call
provideZoneChangeDetection(). - "priority always makes LCP sooner." No. Here the photo arrived early anyway, and waited for hydration to finish.
Practice exercises
Answer each one first, then open the hints if you need them.
When Asha typed the y of honey, 200 reviews were shown. What does it print?
// The eager page checks App, Header, CartBadge, AddButton, FilterBox and ReviewList,
// then a Review, a Stars and five Stars for each review shown.
const reviewsShown = 200;
console.log(6 + reviewsShown * 7);1406. After honey, 200 reviews were left. The counting build counted exactly 1,406 templates for the y: 6, plus 7 for each review.
Angular compares an OnPush input with Object.is. What does it print?
const shown = ["Asha", "Ravi"];
const before = shown;
shown.push("Meera"); // a new review, added in place
console.log(Object.is(before, shown));true. It's the same array. An OnPush ReviewList given this array would see no new input and skip its check, so Meera's review wouldn't show. Pass a new array instead, or use a signal.
On the hydrate-on-interaction page, how long after Asha's click did Helpful show as pressed, in ms?
422.7 ms. The click started the download of the reviews' code. Angular then hydrated the reviews and replayed the click. With hydrate on viewport, the code was already there, and it took a few ms.
On the Eager page, how many templates ran for the o?
2,456. After ho, 350 reviews were left: 6 templates, plus 350 times 7. The OnPush and signals pages ran 3.
Open the course's trace of the Eager page in Chrome's DevTools. It's a development build with enableProfiling(), and holds one click on Add to cart. Which component has the most entries in the Angular track?
_Star, with Star_Template (update) beside it. Five stars in every review makes Star the most checked component. In this development build, the class names start with _.
Meera must keep incremental hydration off for now, but wants early clicks kept. Which function does she add to provideClientHydration()?
withEventReplay(). Pass it with withNoIncrementalHydration() to provideClientHydration(). Incremental hydration turns event replay on by itself, so without it you add replay by hand.
Check your understanding
For each question, ask two things: what did Angular check or load, and when?
Question 1 of 7In Angular 22, a component sets no changeDetection. Which strategy does it get?
Choose an answer to see the explanation.
Question 2 of 7On the OnPush page, why did the cart badge keep saying Cart (0) after the click?
Choose an answer to see the explanation.
Question 3 of 7With the cart in a signal, which templates ran for the click on Add to cart?
Choose an answer to see the explanation.
Question 4 of 7On the server, what does @defer (on viewport) render for the reviews?
Choose an answer to see the explanation.
Question 5 of 7What did hydrate on viewport change, next to plain on viewport?
Choose an answer to see the explanation.
Question 6 of 7Why didn't NgOptimizedImage's priority make the photo show sooner on the recorded page?
Choose an answer to see the explanation.
Question 7 of 7What happened to Asha's early click on the page as the update left it?
Choose an answer to see the explanation.
Key takeaways
- Change detection runs your templates again to find what changed. Keep slow work, like Markdown, out of templates.
- Eager components are checked every time. Since Angular 22, OnPush is the default; ng update adds Eager to old components.
- OnPush skips components with no new input and no event inside. A value changed elsewhere needs an input or a signal.
- A signal tells Angular exactly which template to refresh. Since Angular 21, apps run without zone.js unless they ask for it.
- @defer moves a block's code to a later file, but the server sends only its placeholder.
- A hydrate trigger sends the block's HTML now and its code later. It comes with event replay, which keeps early clicks.
- NgOptimizedImage's priority asks for the photo first. LCP still waits for a free main thread.
Remember the one-liner.
Check only what changed, send only what's needed now, and wake the rest when someone reaches it.
Sources
The facts on this page come from these docs and source files:
- Runtime performance optimization, Angular.
- Slow computations, Angular.
- Skipping component subtrees, Angular.
- constants.ts, Angular 22.2.6, Angular source.
- definition.ts, Angular 22.2.6, Angular source.
- change_detection.ts, Angular 22.2.6, Angular source.
- callback_scheduler.ts, Angular 22.2.6, Angular source.
- Signals, Angular.
- mark_view_dirty.ts, Angular 22.2.6, Angular source.
- view_utils.ts, Angular 22.2.6, Angular source.
- Angular without ZoneJS (Zoneless), Angular.
- create_application.ts, Angular 22.2.6, Angular source.
- Profiling with the Chrome DevTools, Angular.
- Deferred loading with @defer, Angular.
- Incremental Hydration, Angular.
- Hydration, Angular.
- hydration.ts, Angular 22.2.6, Angular source.
- hydration/api.ts, Angular 22.2.6, Angular source.
- Getting started with NgOptimizedImage, Angular.
- ng_optimized_image.ts, Angular 22.2.6, Angular source.
Coming next: Vue and Nuxt performance. Reactivity, async components and Nuxt's own tools: the same questions, in another framework.