Sub topic 3
Fixing Slow Change Detection with OnPush
Assume you know Angular components, templates, services, TypeScript, and browser DevTools. You also have an Angular application running in development mode.
Fixing slow change detection with OnPush
Angular change detection checks whether a component's view needs updating. A template method can become expensive because Angular may call it during many checks.
OnPush is a change detection strategy that checks a component when an input reference changes, an event occurs in the component, or a signal used by the template changes. Move repeated calculations into a computed signal or prepare the data before rendering.
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-report',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (row of totals(); track row.id) {
<p>{{ row.name }}: {{ row.total }}</p>
}
`,
})
export class ReportComponent {
rows = signal([
{ id: 1, name: 'January', values: [10, 20, 30] },
{ id: 2, name: 'February', values: [15, 25, 35] },
]);
totals = computed(() =>
this.rows().map((row) => ({
id: row.id,
name: row.name,
total: row.values.reduce((sum, value) => sum + value, 0),
})),
);
}Record a new profile after this change. Compare the number and duration of change detection cycles with the earlier profile. OnPush does not make every calculation faster. It prevents Angular from repeating work when the component's data has not changed.
Inspecting dependency injection
Angular dependency injection supplies services and other dependencies to components and directives. In the component explorer, select a component and inspect its injector information when you need to confirm which provider supplies a value. Check whether a provider belongs to the application, route, or component injector. A component-level provider creates a separate service instance.
Combining DevTools with `ng` utilities
Angular exposes development-only debugging helpers through the ng global. Select an element in the Elements panel, then inspect its component:
const component = ng.getComponent($0);
console.log(component);
component.title = 'Changed from the console';
ng.applyChanges(component);Use this technique to reproduce a UI state without changing application code.
Inspecting the router tree
The router tree shows active routes and their component hierarchy. Use it to verify nested routes, outlets, and route-level providers. A route-level provider can explain why a service instance differs between screens.
Putting it together: a debugging workflow
- Record a baseline profile.
- Identify the slow component or repeated template work.
- Inspect its state, inputs, providers, and active route.
- Replace template calculations with prepared data or computed signals.
- Apply
OnPushwhere the component's data flow supports it. - Reproduce the case with
ngutilities if needed. - Record another profile and verify that the cost decreased.
Exercise
Why should you record a second profile after applying OnPush?
Show answer
The second profile provides evidence that change detection work decreased instead of relying on an assumption.