adev/src/content/best-practices/runtime-performance/skipping-subtrees.md
JavaScript, by default, uses mutable data structures that you can reference from multiple different components. Angular runs change detection over your entire component tree to make sure that the most up-to-date state of your data structures is reflected in the DOM.
Change detection is sufficiently fast for most applications. However, when an application has an especially large component tree, running change detection across the whole application can cause performance issues. You can address this by configuring change detection to only run on a subset of the component tree.
OnPushOnPush is the default change detection strategy in Angular (since v22). It instructs Angular to run change detection for a component subtree only when:
==.@HostListener ) in the subtree's root component or any of its children whether they are using OnPush change detection or not.This section examines several common change detection scenarios to illustrate Angular's behavior.
Eager change detectionIf Angular handles an event within a component with the Eager strategy, the framework executes change detection on the entire component tree. Angular will skip descendant component subtrees with roots using OnPush, which have not received new inputs.
As an example, if we set the change detection strategy of MainComponent to OnPush and the user interacts with a component outside the subtree with root MainComponent, Angular will check all the pink components from the diagram below (AppComponent, HeaderComponent, SearchComponent, ButtonComponent) unless MainComponent receives new inputs:
graph TD;
app[AppComponent] --- header[HeaderComponent];
app --- main["MainComponent (OnPush)"];
header --- search[SearchComponent];
header --- button[ButtonComponent];
main --- login["LoginComponent (OnPush)"];
main --- details[DetailsComponent];
event>Event] --- search
class app checkedNode
class header checkedNode
class button checkedNode
class search checkedNode
class event eventNode
If Angular handles an event within a component with OnPush strategy, the framework will execute change detection within the entire component tree. Angular will ignore component subtrees with roots using OnPush, which have not received new inputs and are outside the component which handled the event.
As an example, if Angular handles an event within MainComponent, the framework will run change detection in the entire component tree. Angular will ignore the subtree with root LoginComponent because it has OnPush and the event happened outside of its scope.
graph TD;
app[AppComponent] --- header[HeaderComponent];
app --- main["MainComponent (OnPush)"];
header --- search[SearchComponent];
header --- button[ButtonComponent];
main --- login["LoginComponent (OnPush)"];
main --- details[DetailsComponent];
event>Event] --- main
class app checkedNode
class header checkedNode
class button checkedNode
class search checkedNode
class main checkedNode
class details checkedNode
class event eventNode
If Angular handles an event in a component with OnPush, the framework will execute change detection in the entire component tree, including the component’s ancestors.
As an example, in the diagram below, Angular handles an event in LoginComponent which uses OnPush. Angular will invoke change detection in the entire component subtree including MainComponent (LoginComponent’s parent), even though MainComponent has OnPush as well. Angular checks MainComponent as well because LoginComponent is part of its view.
graph TD;
app[AppComponent] --- header[HeaderComponent];
app --- main["MainComponent (OnPush)"];
header --- search[SearchComponent];
header --- button[ButtonComponent];
main --- login["LoginComponent (OnPush)"];
main --- details[DetailsComponent];
event>Event] --- login
class app checkedNode
class header checkedNode
class button checkedNode
class search checkedNode
class login checkedNode
class main checkedNode
class details checkedNode
class event eventNode
Angular will run change detection within a child component with OnPush when setting an input property as result of a template binding.
For example, in the diagram below, AppComponent passes a new input to MainComponent, which has OnPush. Angular will run change detection in MainComponent but will not run change detection in LoginComponent, which also has OnPush, unless it receives new inputs as well.
graph TD;
app[AppComponent] --- header[HeaderComponent];
app --- main["MainComponent (OnPush)"];
header --- search[SearchComponent];
header --- button[ButtonComponent];
main --- login["LoginComponent (OnPush)"];
main --- details[DetailsComponent];
event>Parent passes new input to MainComponent]
class app checkedNode
class header checkedNode
class button checkedNode
class search checkedNode
class main checkedNode
class details checkedNode
class event eventNode
@ViewChild or @ContentChild to get a reference to a component in TypeScript and manually modify an @Input property, Angular will not automatically run change detection for OnPush components. If you need Angular to run change detection, you can inject ChangeDetectorRef in your component and call changeDetectorRef.markForCheck() to tell Angular to schedule a change detection.