# Measuring Angular performance

Open this when you need the exact tool invocation and the number to report. Verified 2026-09 against https://angular.dev/tools/devtools, https://angular.dev/tools/cli/build and https://web.dev/articles/inp.

## 1. Rule zero: production build, stated conditions

```bash
ng build                        # production configuration by default
npx http-server dist/<app>/browser -p 4300 -c-1
```

Dev-mode runs change detection twice per cycle to surface NG0100, ships unminified code and skips optimizations — a dev measurement is not evidence about production. Record with the number: build mode, CPU throttle, network profile, browser version, and whether extensions were disabled.

## 2. Angular DevTools (browser extension)

- **Profiler tab** → record → perform exactly one interaction → stop. Read: how many change-detection cycles that one interaction caused, and the bar chart of per-component time. Two cycles for a click is normal; twenty means something re-enters.
- **Components tab** → select a node to inspect its inputs and signal values live; useful for confirming that an input reference changes when you did not expect it to.
- **Signal graph** → shows which signals a component reads and what depends on them. This is the fastest way to find a `computed()` that depends on more than it needs and therefore recomputes on unrelated writes.

What to report: "one keystroke in the filter box triggers 14 CD cycles; `OrderRow` accounts for 62 ms of the 80 ms frame."

## 3. Bundle analysis

```bash
ng build --stats-json
npx esbuild-visualizer --metadata dist/<app>/stats.json --open
```

The `application` builder emits an esbuild metafile, so any esbuild metafile viewer works (`esbuild-visualizer`, esbuild's own analyzer, `source-map-explorer` on the emitted maps).

Read it top down and ask, per entry over ~30 kB: does the *initial* chunk need this at all? Common answers:

| Finding | Fix |
|---|---|
| A whole date library for one format call | `Intl.DateTimeFormat`, or `date-fns` with per-function imports |
| All of lodash | Import the single function, or write the ten lines |
| An icon font or full icon package | Inline the used SVGs |
| Chart/editor library in the initial chunk | `@defer` it, or lazy-load the route that uses it |
| Material imported in the root component | Import per component that actually uses it |
| Two copies of the same library | `npm ls <pkg>` — a transitive dependency pinned a different major |

Budgets in `angular.json`:

```json
"budgets": [
  { "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" },
  { "type": "anyComponentStyle", "maximumWarning": "4kB", "maximumError": "8kB" }
]
```

Set `maximumError` to today's size plus a small margin rather than a round number: a budget that is 40 % above reality never fires.

## 4. Core Web Vitals, and INP in particular

Lab (Lighthouse) tells you whether a page *can* be fast; field data tells you whether it *is*. Collect both.

```ts
// main.ts (or a small reporting service) — npm i web-vitals
import { onCLS, onINP, onLCP, type Metric } from 'web-vitals';

const report = (metric: Metric) => navigator.sendBeacon('/rum', JSON.stringify(metric));
onLCP(report);
onINP(report);
onCLS(report);
```

`onINP` reports the interaction that produced the worst delay, with the target element in `attribution` when you use `web-vitals/attribution`. That attribution is what turns "INP is 480 ms" into "the filter input's `keyup` handler is the problem", which is the only form a fix can start from.

Thresholds to judge against: LCP ≤ 2.5 s, INP ≤ 200 ms, CLS ≤ 0.1 at the 75th percentile.

**Lighthouse in CI** — `lhci autorun` with assertions committed next to the app, failing the build on regression:

```json
{ "ci": { "assert": { "assertions": {
  "categories:performance": ["error", { "minScore": 0.85 }],
  "largest-contentful-paint": ["error", { "maxNumericValue": 2500 }],
  "total-blocking-time": ["error", { "maxNumericValue": 300 }]
} } } }
```

Run it against the built output on a fixed runner size; Lighthouse scores on shared CI hardware are noisy, so assert on a handful of stable metrics rather than the overall score alone.

## 5. Long tasks and the Performance panel

Record in the Chrome Performance panel with 4–6× CPU throttling. Anything over 50 ms on the main thread is a long task and is what INP measures. In an Angular trace, look for:

- `ApplicationRef.tick` / `refreshView` frames — change detection; go back to the Angular DevTools profiler for the component.
- A single long `evaluate` frame in app code — synchronous work that belongs in a worker or should be chunked.
- Repeated forced reflow warnings — a DOM read after a write in the same frame; move the read into `afterNextRender({ read: … })`.
- Third-party frames — measure with the script blocked to get the honest cost before arguing about it.

## 6. Memory

1. Open the app, navigate to the suspect route, navigate away, force GC (the bin icon), take snapshot A.
2. Repeat the same navigation five times, force GC, take snapshot B.
3. Compare B to A, filtered to "Objects allocated between A and B". Component class names with a growing count are retained instances.
4. Expand a retained instance and read the retainer chain. It almost always ends in a `Subscription`, an `EventTarget` listener, or a root-provided service holding an array.

The two structural causes: a `subscribe()` without `takeUntilDestroyed()`, and state kept in a root-provided service that should have been provided on the route.

## 7. Reporting template

```
Baseline: INP p75 = 460 ms (field, 7 days, Chrome, all devices)
          LCP p75 = 3.1 s
Bucket:   template work during change detection
Evidence: Angular DevTools — one keystroke = 14 CD cycles; OrderRow 62 ms of an 80 ms frame.
          web-vitals attribution names the filter input as the INP target on 78 % of samples.
Fix:      computed() view model per row + debounceTime(300) on the filter signal.
Verify:   one keystroke = 2 CD cycles; INP p75 = 140 ms after 7 days.
```
