feat(challenge33): about performance

This commit is contained in:
thomas
2023-09-12 11:39:36 +02:00
parent 323c38e334
commit bc1c772820
23 changed files with 270 additions and 323 deletions

View File

@@ -8,13 +8,13 @@ In this series of challenges, you will learn how to optimize and enhance the per
The first step is to download the [Angular DevTools Chrome extention](https://chrome.google.com/webstore/detail/angular-devtools/ienfalfjdbdpebioblfackkekamfmbnh) if you haven't already done so. This extension allows you to profile your application and detect performance issues.
In this challenge, we will explore the differences and impacts of using `ChangeDetectionStrategy.Default` versus `ChangeDetectionStrategy.OnPush`. To provide a clearer demonstration, I have added counters to each component and each row in our application. However, in real-world scenarios, you may not have such counters. This is where the Angular DevTool profiler comes to the rescue.
In this challenge, we will explore the differences and impacts of using `ChangeDetectionStrategy.Default` versus `ChangeDetectionStrategy.OnPush`. To provide a clearer demonstration, I have added color enlightment to each component and each row in our application. However, in real-world scenarios, you will not have such visualization. This is where the Angular DevTool profiler comes to the rescue.
Start by serving this application by running: `npx nx serve performance-default-onpush` inside your terminal. Then open Chrome DevTool by pressing **F12** and switch to the Angular Tab. From there you can select the Profiler tab as shown below.
![profiler tab](./img/profiler-tab.png 'Profiler tab')
Start profiling your application and type some letters inside the input field. You will notice that each number is increasing and the profiler will show you a lot of change detection cycle.
Start profiling your application and type some letters inside the input field. You will notice that each element of your application will flash at each change detection cycle and the profiler will show you a bar for each change detection cycle.
If you click on one of the bars (indicated by the yellow arrow on the picture below), you can see that `PersonListComponent`, `RandomComponent` and all the `MatListItem` are impacted by the change detection cycle, even when we only interact with the input field.

View File

@@ -23,7 +23,8 @@
"apps/performance/default-onpush/src/styles.scss",
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css"
],
"scripts": []
"scripts": [],
"allowedCommonJsDependencies": ["seedrandom"]
},
"configurations": {
"production": {

View File

@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { randFirstName } from '@ngneat/falso';
import { PersonListComponent } from './person-list.component';
import { Persons } from './persons';
import { RandomComponent } from './random.component';
@Component({
@@ -11,12 +11,12 @@ import { RandomComponent } from './random.component';
<app-random />
<div class="flex">
<app-person-list [data]="personList" title="List 1" />
<app-person-list [data]="person2List" title="List 2" />
<app-person-list [names]="girlList" title="Female" />
<app-person-list [names]="boyList" title="Male" />
</div>
`,
})
export class AppComponent {
personList = [...Persons];
person2List = [...Persons];
girlList = randFirstName({ gender: 'female', length: 10 });
boyList = randFirstName({ gender: 'male', length: 10 });
}

View File

@@ -1,12 +1,12 @@
import { Component, Input } from '@angular/core';
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatChipsModule } from '@angular/material/chips';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatListModule } from '@angular/material/list';
import { Person } from './person';
@Component({
selector: 'app-person-list',
@@ -18,10 +18,11 @@ import { Person } from './person';
MatFormFieldModule,
MatInputModule,
MatChipsModule,
CDFlashingDirective,
],
template: `
<h1 class="font-semibold text-center" title="Title">
{{ title }}
<h1 cd-flash class="font-semibold text-center" title="Title">
{{ title | titlecase }}
</h1>
<mat-form-field class="w-4/5">
@@ -34,18 +35,18 @@ import { Person } from './person';
</mat-form-field>
<mat-list class="flex w-full">
<div *ngIf="data?.length === 0" class="empty-list-label">Empty list</div>
<mat-list-item *ngFor="let item of data">
<div *ngIf="names?.length === 0" class="empty-list-label">Empty list</div>
<mat-list-item
*ngFor="let name of names"
cd-flash
class="text-orange-500">
<div MatListItemLine class="flex justify-between">
<h3 title="Name">
{{ item.label }}
{{ name }}
</h3>
<div class="px-4 py-2 rounded-3xl bg-gray-400">
{{ count(item) }}
</div>
</div>
</mat-list-item>
<mat-divider *ngIf="data?.length !== 0"></mat-divider>
<mat-divider *ngIf="names?.length !== 0"></mat-divider>
</mat-list>
`,
host: {
@@ -53,20 +54,15 @@ import { Person } from './person';
},
})
export class PersonListComponent {
@Input() data: Person[] | null = null;
@Input() names: string[] = [];
@Input() title = '';
label = '';
handleKey(event: any) {
if (event.keyCode === 13) {
this.data?.unshift({ label: this.label, num: 0 });
this.names?.unshift(this.label);
this.label = '';
}
}
count(person: Person) {
person.num++;
return person.num;
}
}

View File

@@ -1,4 +0,0 @@
export interface Person {
label: string;
num: number;
}

View File

@@ -1,284 +0,0 @@
import { Person } from './person';
export const Persons: Person[] = [
{
label: 'Nettle',
num: 0,
},
{
label: 'Rosalie',
num: 0,
},
{
label: 'Rhona',
num: 0,
},
{
label: 'Talyah',
num: 0,
},
{
label: 'Fancie',
num: 0,
},
{
label: 'Kari',
num: 0,
},
{
label: 'Caresa',
num: 0,
},
{
label: 'Gerta',
num: 0,
},
{
label: 'Modestine',
num: 0,
},
{
label: 'Emogene',
num: 0,
},
{
label: 'Henryetta',
num: 0,
},
{
label: 'Darelle',
num: 0,
},
{
label: 'Clementine',
num: 0,
},
{
label: 'Arabella',
num: 0,
},
{
label: 'Constance',
num: 0,
},
{
label: 'Josey',
num: 0,
},
{
label: 'Talia',
num: 0,
},
{
label: 'Loleta',
num: 0,
},
{
label: 'Tedda',
num: 0,
},
{
label: 'Francine',
num: 0,
},
{
label: 'Kittie',
num: 0,
},
{
label: 'Merry',
num: 0,
},
{
label: 'Lexi',
num: 0,
},
{
label: 'Dorie',
num: 0,
},
{
label: 'Daron',
num: 0,
},
{
label: 'Bella',
num: 0,
},
{
label: 'Ashien',
num: 0,
},
{
label: 'Hyacinthia',
num: 0,
},
{
label: 'Beatrix',
num: 0,
},
{
label: 'Tamiko',
num: 0,
},
{
label: 'Gusty',
num: 0,
},
{
label: 'Talyah',
num: 0,
},
{
label: 'Cynthy',
num: 0,
},
{
label: 'Trudy',
num: 0,
},
{
label: 'Katharine',
num: 0,
},
{
label: 'Kelci',
num: 0,
},
{
label: 'Allyce',
num: 0,
},
{
label: 'Dorthea',
num: 0,
},
{
label: 'Patty',
num: 0,
},
{
label: 'Fernandina',
num: 0,
},
{
label: 'Fanya',
num: 0,
},
{
label: 'Lauralee',
num: 0,
},
{
label: 'Nanice',
num: 0,
},
{
label: 'Maureen',
num: 0,
},
{
label: 'Wilmette',
num: 0,
},
{
label: 'Ellie',
num: 0,
},
{
label: 'Maybelle',
num: 0,
},
{
label: 'Angelina',
num: 0,
},
{
label: 'Dawn',
num: 0,
},
{
label: 'Arlene',
num: 0,
},
{
label: 'Maryanne',
num: 0,
},
{
label: 'Alyda',
num: 0,
},
{
label: 'Alisun',
num: 0,
},
{
label: 'Liana',
num: 0,
},
{
label: 'Alejandrina',
num: 0,
},
{
label: 'Costanza',
num: 0,
},
{
label: 'Wendie',
num: 0,
},
{
label: 'Rosalinda',
num: 0,
},
{
label: 'Annabela',
num: 0,
},
{
label: 'Kelsi',
num: 0,
},
{
label: 'Morgana',
num: 0,
},
{
label: 'Gabriel',
num: 0,
},
{
label: 'Cherianne',
num: 0,
},
{
label: 'Belia',
num: 0,
},
{
label: 'Jillayne',
num: 0,
},
{
label: 'Deerdre',
num: 0,
},
{
label: 'Tedra',
num: 0,
},
{
label: 'Reine',
num: 0,
},
{
label: 'Anett',
num: 0,
},
{
label: 'Donielle',
num: 0,
},
];

View File

@@ -1,13 +1,10 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { Component } from '@angular/core';
@Component({
selector: 'app-random',
standalone: true,
template: `I do nothing but I'm here: {{ count() }}`,
template: `<div cd-flash>I do nothing but I'm here</div>`,
imports: [CDFlashingDirective],
})
export class RandomComponent {
counter = 0;
count() {
return this.counter++;
}
}
export class RandomComponent {}

View File

@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>service-worker</title>
<title>Default OnPush</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />

View File

@@ -1,6 +1,9 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts"],
"include": [
"src/**/*.ts",
"../../../libs/shared/directives/src/lib/cd-flashing.directive.ts"
],
"compilerOptions": {
"types": []
}

View File

@@ -0,0 +1,36 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "lib",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "lib",
"style": "kebab-case"
}
]
},
"extends": [
"plugin:@nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
]
},
{
"files": ["*.html"],
"extends": ["plugin:@nx/angular-template"],
"rules": {}
}
]
}

View File

@@ -0,0 +1,7 @@
# shared-directives
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test shared-directives` to execute the unit tests.

View File

@@ -0,0 +1,22 @@
/* eslint-disable */
export default {
displayName: 'shared-directives',
preset: '../../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
coverageDirectory: '../../../coverage/libs/shared/directives',
transform: {
'^.+\\.(ts|mjs|js|html)$': [
'jest-preset-angular',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
],
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
};

View File

@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/libs/shared/directives",
"lib": {
"entryFile": "src/index.ts"
}
}

View File

@@ -0,0 +1,12 @@
{
"name": "@angular-challenges/shared/directives",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^16.1.0",
"@angular/core": "^16.1.0"
},
"dependencies": {
"tslib": "^2.3.0"
},
"sideEffects": false
}

View File

@@ -0,0 +1,50 @@
{
"name": "shared-directives",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/shared/directives/src",
"prefix": "lib",
"tags": [],
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/angular:ng-packagr-lite",
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
"options": {
"project": "libs/shared/directives/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "libs/shared/directives/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "libs/shared/directives/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/shared/directives/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/shared/directives/**/*.ts",
"libs/shared/directives/**/*.html"
]
}
}
}
}

View File

@@ -0,0 +1 @@
export * from './lib/cd-flashing.directive';

View File

@@ -0,0 +1,23 @@
/* eslint-disable @angular-eslint/directive-selector */
import { Directive, DoCheck, ElementRef, NgZone } from '@angular/core';
@Directive({
selector: '[cd-flash]',
standalone: true,
})
export class CDFlashingDirective implements DoCheck {
constructor(private elementRef: ElementRef, private zone: NgZone) {}
ngDoCheck(): void {
this.cdRan();
}
public cdRan(): void {
this.zone.runOutsideAngular(() => {
this.elementRef.nativeElement.classList.add('!bg-orange-500');
setTimeout(() => {
this.elementRef.nativeElement.classList.remove('!bg-orange-500');
}, 1000);
});
}
}

View File

@@ -0,0 +1,8 @@
// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
globalThis.ngJest = {
testEnvironmentOptions: {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
};
import 'jest-preset-angular/setup-jest';

View File

@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es2022",
"useDefineForClassFields": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"extends": "../../../tsconfig.base.json",
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

View File

@@ -0,0 +1,17 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": []
},
"exclude": [
"src/**/*.spec.ts",
"src/test-setup.ts",
"jest.config.ts",
"src/**/*.test.ts"
],
"include": ["src/**/*.ts"]
}

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {}
}

View File

@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"target": "es2016",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}

View File

@@ -63,6 +63,9 @@
"@angular-challenges/ngrx-notification/model": [
"libs/ngrx-notification/model/src/index.ts"
],
"@angular-challenges/shared/directives": [
"libs/shared/directives/src/index.ts"
],
"@angular-challenges/shared/utils": ["libs/shared/utils/src/index.ts"],
"@angular-challenges/testing-table/backend": [
"libs/testing-table/backend/src/index.ts"