Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance: all - enable ChangeDetectionStrategy.OnPush wherever possible #1331

Closed
christophercr opened this issue Jun 11, 2019 · 0 comments · Fixed by #1334
Closed

performance: all - enable ChangeDetectionStrategy.OnPush wherever possible #1331

christophercr opened this issue Jun 11, 2019 · 0 comments · Fixed by #1334
Assignees
Labels
Milestone

Comments

@christophercr
Copy link
Collaborator

christophercr commented Jun 11, 2019

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/NationalBankBelgium/stark/blob/master/CONTRIBUTING.md#got-a-question-or-problem

Current behavior

Angular runs a lot of change detection cycles whenever something happens in the UI in order to know whether the view should be updated ('dirty checking"). This means that the app could get serious performance issues if there are a lot of components and Angular runs the dirty checking for each of them.

Expected behavior

Dumb components (those that just rely in their inputs) should use the ChangeDetectionStrategy.OnPush so that Angular just runs the dirty checking in very specific cases or when it is triggered manually.

This will reduce the amount of checks automatically executed by Angular and ultimately improve the performance of the App.

See A Comprehensive Guide to Angular onPush Change Detection Strategy

Minimal reproduction of the problem with instructions

In Showcase:

  1. refactor the ExampleViewerComponent and implement a setter and getter for the exampleTitle input and add a console.log in the getter:
@Input()
public get exampleTitle(): string {
	console.log("==> exampleTitle GETTER");
	return this._exampleTitle;
}
public set exampleTitle(value: string) {
	this._exampleTitle = value;
}

private _exampleTitle = "undefined";
  1. run the Showcase app and navigate to any of the demo pages where the ExampleViewerComponent is used.
  2. open the browser console. The message ==> exampleTitle GETTER is logged multiple times as you interact with the UI (mouse hover, click, etc). This is because Angular checks this property for changes in order to refresh the template if needed.

In stark-ui:

  1. refactor the StarkAppLogoutComponent and implement a setter and getter for the icon input and add a console.log in the getter:
@Input()
public get icon(): "power" | string {
	console.log("==> icon GETTER");
	return this._icon;
}
public set icon(value: "power" | string) {
	this._icon = value;
}
	
private _icon: "power" | string = "power";
  1. update the stark-ui dependency in the Showcase to take the changes done in the previous step.
  2. run the Showcase app.
  3. open the browser console. The message ==> icon GETTER is logged multiple times as you interact with the UI (mouse hover, click, etc) and also when you navigate to any page in the app. This is because Angular checks this property for changes in order to refresh the template if needed.

What is the motivation / use case for changing the behavior?

Performant components/apps.

Environment


Stark version: 10.0.0-beta.8

@christophercr christophercr added this to the 10.0.0-rc.0 milestone Jun 11, 2019
@christophercr christophercr self-assigned this Jun 11, 2019
christophercr added a commit to christophercr/stark that referenced this issue Jun 12, 2019
…o prevent Angular from running unnecessary change detection cycles

ISSUES CLOSED: NationalBankBelgium#1331
christophercr added a commit to christophercr/stark that referenced this issue Jun 13, 2019
…o prevent Angular from running unnecessary change detection cycles

ISSUES CLOSED: NationalBankBelgium#1331
christophercr added a commit to christophercr/stark that referenced this issue Jun 13, 2019
…o prevent Angular from running unnecessary change detection cycles

ISSUES CLOSED: NationalBankBelgium#1331
christophercr added a commit to christophercr/stark that referenced this issue Jun 13, 2019
…o prevent Angular from running unnecessary change detection cycles

ISSUES CLOSED: NationalBankBelgium#1331
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment