Skip to content

Commit 13f4fb2

Browse files
authored
Use ChangeDetectionStrategy in Angular
Improve Angular performance by using `ChangeDetectionStrategy.OnPush` By default all components are updated on any change. Using the detection strategy the check time was reduced to less than 5% (e.g. person example 20ms to 0.7ms).
1 parent 1200706 commit 13f4fb2

File tree

13 files changed

+72
-52
lines changed

13 files changed

+72
-52
lines changed

packages/angular-material/src/controls/autocomplete.renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component, Input } from '@angular/core';
25+
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
2626
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
2727
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
2828
import {
@@ -90,7 +90,8 @@ import { startWith } from 'rxjs/operators';
9090
<mat-hint>{{ description }}</mat-hint>
9191
<mat-error>{{ error }}</mat-error>
9292
</mat-form-field>
93-
`
93+
`,
94+
changeDetection: ChangeDetectionStrategy.OnPush
9495
})
9596
export class AutocompleteControlRenderer extends JsonFormsControl {
9697
@Input() options: string[];

packages/angular-material/src/controls/boolean.renderer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ViewRef} from '@angular/core';
2626
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
2727
import { isBooleanControl, RankedTester, rankWith } from '@jsonforms/core';
2828

@@ -46,14 +46,21 @@ import { isBooleanControl, RankedTester, rankWith } from '@jsonforms/core';
4646
<mat-hint class="mat-caption">{{ description }}</mat-hint>
4747
<mat-error class="mat-caption">{{ error }}</mat-error>
4848
</div>
49-
`
49+
`,
50+
changeDetection: ChangeDetectionStrategy.OnPush
5051
})
5152
export class BooleanControlRenderer extends JsonFormsControl {
52-
constructor(jsonformsService: JsonFormsAngularService) {
53+
constructor(jsonformsService: JsonFormsAngularService, private changeDetectionRef: ChangeDetectorRef) {
5354
super(jsonformsService);
5455
}
5556
isChecked = () => this.data || false;
5657
getEventValue = (event: any) => event.checked;
58+
59+
mapAdditionalProps() {
60+
if (!(this.changeDetectionRef as ViewRef).destroyed) {
61+
this.changeDetectionRef.markForCheck();
62+
}
63+
}
5764
}
5865

5966
export const booleanControlTester: RankedTester = rankWith(2, isBooleanControl);

packages/angular-material/src/controls/date.renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { Component, ChangeDetectionStrategy } from '@angular/core';
2626
import {
2727
getLocale,
2828
isDateControl,
@@ -52,7 +52,8 @@ import { DateAdapter, NativeDateAdapter } from '@angular/material/core';
5252
<mat-hint>{{ description }}</mat-hint>
5353
<mat-error>{{ error }}</mat-error>
5454
</mat-form-field>
55-
`
55+
`,
56+
changeDetection: ChangeDetectionStrategy.OnPush
5657
})
5758
export class DateControlRenderer extends JsonFormsControl {
5859
constructor(

packages/angular-material/src/controls/number.renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2626
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
2727
import {
2828
getLocale,
@@ -51,7 +51,8 @@ import {
5151
<mat-hint>{{ description }}</mat-hint>
5252
<mat-error>{{ error }}</mat-error>
5353
</mat-form-field>
54-
`
54+
`,
55+
changeDetection: ChangeDetectionStrategy.OnPush
5556
})
5657
export class NumberControlRenderer extends JsonFormsControl {
5758
private readonly MAXIMUM_FRACTIONAL_DIGITS = 20;

packages/angular-material/src/controls/text.renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2626
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
2727
import { isStringControl, RankedTester, rankWith } from '@jsonforms/core';
2828

@@ -41,7 +41,8 @@ import { isStringControl, RankedTester, rankWith } from '@jsonforms/core';
4141
<mat-hint>{{ description }}</mat-hint>
4242
<mat-error>{{ error }}</mat-error>
4343
</mat-form-field>
44-
`
44+
`,
45+
changeDetection: ChangeDetectionStrategy.OnPush
4546
})
4647
export class TextControlRenderer extends JsonFormsControl {
4748
constructor(jsonformsService: JsonFormsAngularService) {

packages/angular-material/src/controls/textarea.renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2626
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
2727
import { isMultiLineControl, RankedTester, rankWith } from '@jsonforms/core';
2828

@@ -40,7 +40,8 @@ import { isMultiLineControl, RankedTester, rankWith } from '@jsonforms/core';
4040
<mat-hint>{{ description }}</mat-hint>
4141
<mat-error>{{ error }}</mat-error>
4242
</mat-form-field>
43-
`
43+
`,
44+
changeDetection: ChangeDetectionStrategy.OnPush
4445
})
4546
export class TextAreaRenderer extends JsonFormsControl {
4647
constructor(jsonformsService: JsonFormsAngularService) {

packages/angular-material/src/layouts/group-layout.renderer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
2626
import { GroupLayout, RankedTester, rankWith, uiTypeIs } from '@jsonforms/core';
2727
import { LayoutRenderer } from './layout.renderer';
2828
import { JsonFormsAngularService } from '@jsonforms/angular';
@@ -36,11 +36,12 @@ import { JsonFormsAngularService } from '@jsonforms/angular';
3636
<jsonforms-outlet [renderProps]="props"></jsonforms-outlet>
3737
</div>
3838
</mat-card>
39-
`
39+
`,
40+
changeDetection: ChangeDetectionStrategy.OnPush
4041
})
4142
export class GroupLayoutRenderer extends LayoutRenderer<GroupLayout> {
42-
constructor(jsonFormsService: JsonFormsAngularService) {
43-
super(jsonFormsService);
43+
constructor(jsonFormsService: JsonFormsAngularService, changeDetectionRef: ChangeDetectorRef) {
44+
super(jsonFormsService, changeDetectionRef);
4445
}
4546
}
4647
export const groupLayoutTester: RankedTester = rankWith(1, uiTypeIs('Group'));

packages/angular-material/src/layouts/horizontal-layout.renderer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
2626
import {
2727
HorizontalLayout,
2828
RankedTester,
@@ -45,11 +45,12 @@ import { JsonFormsAngularService } from '@jsonforms/angular';
4545
<jsonforms-outlet [renderProps]="props"></jsonforms-outlet>
4646
</div>
4747
</div>
48-
`
48+
`,
49+
changeDetection: ChangeDetectionStrategy.OnPush
4950
})
5051
export class HorizontalLayoutRenderer extends LayoutRenderer<HorizontalLayout> {
51-
constructor(jsonFormsService: JsonFormsAngularService) {
52-
super(jsonFormsService);
52+
constructor(jsonFormsService: JsonFormsAngularService, changeDetectionRef: ChangeDetectorRef) {
53+
super(jsonFormsService, changeDetectionRef);
5354
}
5455
}
5556
export const horizontalLayoutTester: RankedTester = rankWith(

packages/angular-material/src/layouts/layout.renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { OnDestroy, OnInit } from '@angular/core';
25+
import { OnDestroy, OnInit, ChangeDetectorRef } from '@angular/core';
2626
import {
2727
JsonFormsAngularService,
2828
JsonFormsBaseRenderer
@@ -41,7 +41,7 @@ export class LayoutRenderer<T extends Layout> extends JsonFormsBaseRenderer<T>
4141
hidden: boolean;
4242
private subscription: Subscription;
4343

44-
constructor(private jsonFormsService: JsonFormsAngularService) {
44+
constructor(private jsonFormsService: JsonFormsAngularService, protected changeDetectionRef: ChangeDetectorRef) {
4545
super();
4646
}
4747

@@ -50,6 +50,7 @@ export class LayoutRenderer<T extends Layout> extends JsonFormsBaseRenderer<T>
5050
next: (state: JsonFormsState) => {
5151
const props = mapStateToLayoutProps(state, this.getOwnProps());
5252
this.hidden = !props.visible;
53+
this.changeDetectionRef.markForCheck();
5354
}
5455
});
5556
}

packages/angular-material/src/layouts/vertical-layout.renderer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { Component } from '@angular/core';
25+
import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
2626
import {
2727
RankedTester,
2828
rankWith,
@@ -40,11 +40,12 @@ import { JsonFormsAngularService } from '@jsonforms/angular';
4040
<jsonforms-outlet [renderProps]="props"></jsonforms-outlet>
4141
</div>
4242
</div>
43-
`
43+
`,
44+
changeDetection: ChangeDetectionStrategy.OnPush
4445
})
4546
export class VerticalLayoutRenderer extends LayoutRenderer<VerticalLayout> {
46-
constructor(jsonFormsService: JsonFormsAngularService) {
47-
super(jsonFormsService);
47+
constructor(jsonFormsService: JsonFormsAngularService, changeDetectionRef: ChangeDetectorRef) {
48+
super(jsonFormsService, changeDetectionRef);
4849
}
4950
}
5051
export const verticalLayoutTester: RankedTester = rankWith(

0 commit comments

Comments
 (0)