completefrontend
Open lab My progress
THE WEB PERFORMANCE FIELD GUIDE

Angular performance

Measure what OnPush, signals, zoneless change detection, @defer, incremental hydration and NgOptimizedImage change on a real Angular shop.

By the end, you can
  • 01
    Count what change detection checksSee why Eager components check every template, and what OnPush skips, in templates run and in time.
  • 02
    Tell Angular exactly what changedUse signals so only the readers refresh, and run an app without zone.js.
  • 03
    Send 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:

Real frames of the reviews page around Asha's click on Add to cart. Before the click the badge says Cart (0). The first new frame after it: Eager, 47.4 ms later, Cart (1). OnPush, 17.6 ms later, still Cart (0). Signals, 23.9 ms later, Cart (1).Before the clickThe reviews page before the click: the badge says Cart (0)Cart (0)Eager, 47.4 msThe Eager page after the click: the badge says Cart (1)Cart (1)OnPush, 17.6 msThe OnPush page after the click: the badge still says Cart (0)Cart (0)Signals, 23.9 msThe signals page after the click: the badge says Cart (1)Cart (1)
Real frames from the course's recording, cropped to the top of Asha's screen. Above each frame: the page, and how long after the click Chrome drew it. Below: what the cart badge says.

The OnPush page answered fastest, but its badge never changed. Here is what each page did for that one click:

One click on three reviews pagesPop out in the code editor (opens in a new tab)JavaScript
// 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). Then OnPush, 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.

Real-life analogyA teacher who marks every book again

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
The main idea

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 build writes.
  • 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:

review.ts on the Eager pageTypeScript
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. @Component tells Angular its name, its settings and its template.
  • Line 8 is the line ng update added: 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 checked for one clickPop out in the code editor (opens in a new tab)JavaScript
// 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 prints toHtml 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.

The component templates Angular ran for Asha's click on Add to cart, on three pages. Eager: 7,006 templates ran: App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 1,000, Stars 1,000, Star 5,000. OnPush: 2 templates ran: App 1, AddButton 1. Signals: 3 templates ran: App 1, CartBadge 1, AddButton 1. Every other component was skipped.Eager: 7,006 ranApp 1Header 1CartBadge 1AddButton 1FilterBox 1ReviewList 1Review 1,000Stars 1,000Star 5,000OnPush: 2 ranApp 1HeaderCartBadgeAddButton 1FilterBoxReviewListReviewStarsStarSignals: 3 ranApp 1HeaderCartBadge 1AddButton 1FilterBoxReviewListReviewStarsStar
Each card is a component, under the one that draws it, with how many times its template ran for the click. A crossed-out card was skipped.

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

Real-life analogyOnly the books handed in again

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:

CartBadge on the OnPush pageTypeScript
@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.
cart.ts, as the update left itTypeScript
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:

The first key on three pagesPop out in the code editor (opens in a new tab)JavaScript
// 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. Then OnPush: 3 checked, 66.1 ms, 40.5 ms of drawing and Signals: 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:

Playground: what Angular checked on three reviews pages
Eager: one click, then honey
  1. 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)
  2. Click: to the next paint46.9 ms
  3. Key h: templates run6,306 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 900, Stars 900, Star 4,500)
  4. Key h: to the next paint82.5 ms
  5. Key o: templates run2,456 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 350, Stars 350, Star 1,750)
  6. Key o: to the next paint76.2 ms
  7. Key n: templates run1,756 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 250, Stars 250, Star 1,250)
  8. Key n: to the next paint35.1 ms
  9. Key e: templates run1,756 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 250, Stars 250, Star 1,250)
  10. Key e: to the next paint19.2 ms
  11. Key y: templates run1,406 (App 1, Header 1, CartBadge 1, AddButton 1, FilterBox 1, ReviewList 1, Review 200, Stars 200, Star 1,000)
  12. Key y: to the next paint27.5 ms
  13. The badge after the clickCart (1)
  14. zone.js, compressed12.8 kB
Try it yourself
Eager, with zone.js

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.

From the course's recording in Chrome 157, with the CPU slowed 4 times. Counts come from the counting builds, and were the same in all 5 visits. Times come from the production builds, from the visit with the middle INP.

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:

cart.ts with a signalTypeScript
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 count a signal that starts at 0.
  • Line 8 changes it: update takes 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).

Real-life analogyA sticky note on the right page

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:

main.ts, as the update left itTypeScript
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, from polyfills in angular.json.
main.ts, zonelessTypeScript
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:

The reviews in @defer (on viewport)Angular template
@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 viewport is its trigger: load when the block scrolls into view.
  • Line 2 is what the block shows once its code has loaded. app-reviews is 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.

What @defer changedPop out in the code editor (opens in a new tab)JavaScript
// 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:

Real frames of the Mint tea page after the reviews were scrolled into view. With @defer (on viewport): 8.9 ms after the scroll, a Reviews heading, Loading reviews, and the footer; 433.7 ms after it, the reviews, with the footer pushed down. With @defer (hydrate on viewport): 11.7 ms after the scroll, the reviews are already on screen.@defer (on viewport)The @defer page right after the scroll: the Reviews heading, Loading reviews, and the footer8.9 ms after the scrollThe @defer page once the reviews' code arrived: the reviews have pushed the footer down433.7 ms after the scroll(hydrate on viewport)The hydrate on viewport page right after the scroll: the reviews are already there11.7 ms after the scroll
Real frames of Asha's whole screen after the scroll. The arrow joins two frames of the same page. On the right, the server had already sent the reviews.

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.

Real-life analogyThe dessert menu

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.

Playground: six ways of the Mint tea page
Updated: Asha's visits
  1. First byte of the HTML383 ms
  2. Reviews in the first HTMLyes, all 200
  3. JavaScript before the scroll77.7 kB
  4. Page hydrated989.1 ms
  5. LCP, the photo1,040 ms
  6. Reviews' code after the scrollnone
  7. Layout shift after the scroll0
  8. Helpful pressed, after the click6.4 ms
  9. Early click on Add to cartlost: the badge still said Cart (0)
Try it yourself
Updated: withNoIncrementalHydration(), a plain img, the reviews in main.js

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.

From the course's recording in Chrome 157, with the CPU slowed 4 times on an imitated Fast 4G line: times in ms after Asha asked for the page, from the middle of 5 visits of each kind. JavaScript is what went over the network, compressed.

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:

The reviews in @defer (hydrate on viewport)Angular template
@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 viewport says: 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.

The reviews after the scroll, three waysPop out in the code editor (opens in a new tab)JavaScript
// 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. Then hydrate 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:

app.config.ts, as the update left itTypeScript
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 just provideClientHydration().
One early click, three waysPop out in the code editor (opens in a new tab)JavaScript
// 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). Then Default: clicked at 418.9, ready at 883.7, then Cart (1). Last, Hydrate on viewport: clicked at 425, ready at 643, then Cart (1).
Asha's one early click on Add to cart, on three pages, to scale from 200 to 1000 ms after she opened the page. Updated: JavaScript arriving from 294.1 ms to 625 ms, hydrating from 628.1 ms to 902.1 ms, the click at 437.1 ms, the click was lost. Default: JavaScript arriving from 280.6 ms to 613.6 ms, hydrating from 617.6 ms to 899.6 ms, the click at 418.9 ms, the badge said Cart (1) at 903.9 ms. Hydrate on viewport: JavaScript arriving from 288 ms to 585.3 ms, hydrating from 588.6 ms to 656.6 ms, the click at 425 ms, the badge said Cart (1) at 660 ms.UpdatedclicklostDefaultclickCart (1)Hydrate on viewportclickCart (1)200 ms400 ms600 ms800 ms1,000 ms
Light bars: the page's JavaScript arriving. Shaded bars: Angular hydrating the page. The thin marks are Asha's click and, when it was kept, the moment the badge changed.

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.

Real-life analogyA shop window and a notepad

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.

The photo, two waysHTML
<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. priority marks it as the photo to load first.

Here is what Meera's server sent for line 2:

The server's HTML for the photoHTML
<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">
The photo on two pagesPop out in the code editor (opens in a new tab)JavaScript
// 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. Then optimized: 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.

Six ways of the Mint tea page
WayJavaScript before the scrollLCPReviews' codeShift after the scrollEarly click
As the update left it77.7 kB1,040 msIn main.js, at load0Lost
Angular 22's hydration default77.7 kB1,020 msIn main.js, at load0Kept
NgOptimizedImage with priority80.0 kB1,024 msIn main.js, at load0Kept
@defer (on viewport)64.3 kB620 ms18.5 kB, after the scroll0.175Kept
@defer (hydrate on viewport)66.6 kB784 ms18.5 kB, after the scroll0Kept
@defer (hydrate on interaction)66.6 kB780 ms18.5 kB, after the Helpful click0Kept

Three patterns stand out:

  • Code: moving the reviews out of main.js cut 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:

Which tool fixes it?
  • 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
Try it yourself
0 of 10 correct

Sort each problem by the Angular tool that fixes it.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

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.

Exercise 1 · Warm-upThe last key on the Eager page

When Asha typed the y of honey, 200 reviews were shown. What does it print?

Starter codePop out in the code editor (opens in a new tab)JavaScript
// 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);

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upA review added in place

    Angular compares an OnPush input with Object.is. What does it print?

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    const shown = ["Asha", "Ravi"];
    const before = shown;
    shown.push("Meera"); // a new review, added in place
    console.log(Object.is(before, shown));

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeThe first Helpful click

      On the hydrate-on-interaction page, how long after Asha's click did Helpful show as pressed, in ms?

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeThe o on the Eager page

        On the Eager page, how many templates ran for the o?

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeThe Angular track

          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?

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeNo incremental hydration, but replay

            Meera must keep incremental hydration off for now, but wants early clicks kept. Which function does she add to provideClientHydration()?

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask two things: what did Angular check or load, and when?

              The Angular performance quiz · 7 questionsScore: first tries count
              1. Question 1 of 7In Angular 22, a component sets no changeDetection. Which strategy does it get?

                Choose an answer to see the explanation.

              2. 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.

              3. 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.

              4. Question 4 of 7On the server, what does @defer (on viewport) render for the reviews?

                Choose an answer to see the explanation.

              5. Question 5 of 7What did hydrate on viewport change, next to plain on viewport?

                Choose an answer to see the explanation.

              6. 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.

              7. 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:

              Coming next: Vue and Nuxt performance. Reactivity, async components and Nuxt's own tools: the same questions, in another framework.

              CompleteFrontend Clear concepts. Working examples.