Skip to content

Commit

Permalink
feat(challenge37): ngfor optimize big list
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalaforge committed Oct 8, 2023
1 parent 6c2da1b commit cdf6df0
Show file tree
Hide file tree
Showing 24 changed files with 418 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If you would like to propose a challenge, this project is open source, so feel f
## Challenges

Check [all 36 challenges](https://angular-challenges.vercel.app/)
Check [all 37 challenges](https://angular-challenges.vercel.app/)

## Contributors ✨

Expand Down
36 changes: 36 additions & 0 deletions apps/performance/ngfor-biglist/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "app",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "app",
"style": "kebab-case"
}
]
},
"extends": [
"plugin:@nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
]
},
{
"files": ["*.html"],
"extends": ["plugin:@nx/angular-template"],
"rules": {}
}
]
}
13 changes: 13 additions & 0 deletions apps/performance/ngfor-biglist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# NgFor optimize big list

> Author: Thomas Laforge
### Run Application

```bash
npx nx serve performance-ngfor-biglist
```

### Documentation and Instruction

Challenge documentation is [here](https://angular-challenges.vercel.app/challenges/angular-performance/37-ngfor-biglist/).
81 changes: 81 additions & 0 deletions apps/performance/ngfor-biglist/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"name": "performance-ngfor-biglist",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"prefix": "app",
"sourceRoot": "apps/performance/ngfor-biglist/src",
"tags": [],
"targets": {
"build": {
"executor": "@angular-devkit/build-angular:browser",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/apps/performance/ngfor-biglist",
"index": "apps/performance/ngfor-biglist/src/index.html",
"main": "apps/performance/ngfor-biglist/src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "apps/performance/ngfor-biglist/tsconfig.app.json",
"assets": [
"apps/performance/ngfor-biglist/src/favicon.ico",
"apps/performance/ngfor-biglist/src/assets"
],
"styles": ["apps/performance/ngfor-biglist/src/styles.scss"],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"executor": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "performance-ngfor-biglist:build:production"
},
"development": {
"browserTarget": "performance-ngfor-biglist:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"executor": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "performance-ngfor-biglist:build"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"apps/performance/ngfor-biglist/**/*.ts",
"apps/performance/ngfor-biglist/**/*.html"
]
}
}
}
}
42 changes: 42 additions & 0 deletions apps/performance/ngfor-biglist/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NgIf } from '@angular/common';
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { generateList } from './generateList';
import { PersonService } from './list.service';
import { PersonListComponent } from './person-list.component';

@Component({
standalone: true,
imports: [
NgIf,
PersonListComponent,
FormsModule,
MatFormFieldModule,
MatInputModule,
],
providers: [PersonService],
selector: 'app-root',
template: `
<button
(click)="loadList.set(true)"
class="border border-black p-2 rounded-md">
Load List
</button>
<app-person-list
*ngIf="loadList()"
class="max-w-2xl w-3/4"
[persons]="persons()" />
`,
host: {
class: 'flex items-center flex-col gap-5',
},
})
export class AppComponent {
readonly persons = signal(generateList());
readonly loadList = signal(false);

label = '';
}
6 changes: 6 additions & 0 deletions apps/performance/ngfor-biglist/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApplicationConfig } from '@angular/core';
import { provideAnimations } from '@angular/platform-browser/animations';

export const appConfig: ApplicationConfig = {
providers: [provideAnimations()],
};
15 changes: 15 additions & 0 deletions apps/performance/ngfor-biglist/src/app/generateList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { randEmail, randFirstName } from '@ngneat/falso';
import { Person } from './person.model';

export function generateList() {
const arr: Person[] = [];

for (let i = 0; i < 100000; i++) {
arr.push({
email: randEmail(),
name: randFirstName(),
});
}

return arr;
}
44 changes: 44 additions & 0 deletions apps/performance/ngfor-biglist/src/app/list.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Injectable, inject, signal } from '@angular/core';
import { randEmail, randFirstName } from '@ngneat/falso';
import { generateList } from './generateList';
import { Person } from './person.model';

@Injectable()
export class PersonService {
private readonly fakeBackend = inject(FakeBackendService);
readonly persons = signal<Person[]>([]);

loadPersons() {
this.persons.set(generateList());
}

deletePerson(email: string) {
this.persons.set(
this.fakeBackend
.returnNewList(this.persons())
.filter((p) => p.email !== email)
);
}

updatePerson(email: string) {
this.persons.set(
this.fakeBackend
.returnNewList(this.persons())
.map((p) => (p.email === email ? { email, name: randFirstName() } : p))
);
}

addPerson(name: string) {
this.persons.set([
{ email: randEmail(), name },
...this.fakeBackend.returnNewList(this.persons()),
]);
}
}

@Injectable({ providedIn: 'root' })
export class FakeBackendService {
returnNewList = (input: Person[]): Person[] => [
...input.map((i) => ({ ...i })),
];
}
30 changes: 30 additions & 0 deletions apps/performance/ngfor-biglist/src/app/person-list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

import { NgForTrackByModule } from '@angular-challenges/shared/directives';
import { CommonModule } from '@angular/common';
import { Person } from './person.model';

@Component({
selector: 'app-person-list',
standalone: true,
imports: [CommonModule, NgForTrackByModule],
template: `
<div class="h-[300px] relative overflow-hidden">
<div class="absolute inset-0 overflow-scroll">
<div
*ngFor="let person of persons; trackByProp: 'email'"
class="flex justify-between items-center border-b h-9">
<h3>{{ person.name }}</h3>
<p>{{ person.email }}</p>
</div>
</div>
</div>
`,
host: {
class: 'w-full flex flex-col',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PersonListComponent {
@Input() persons: Person[] = [];
}
4 changes: 4 additions & 0 deletions apps/performance/ngfor-biglist/src/app/person.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Person {
email: string;
name: string;
}
Empty file.
Binary file added apps/performance/ngfor-biglist/src/favicon.ico
Binary file not shown.
13 changes: 13 additions & 0 deletions apps/performance/ngfor-biglist/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>performance-ngfor-biglist</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>
7 changes: 7 additions & 0 deletions apps/performance/ngfor-biglist/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err)
);
5 changes: 5 additions & 0 deletions apps/performance/ngfor-biglist/src/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

/* You can add global styles to this file, and also import other style files */
14 changes: 14 additions & 0 deletions apps/performance/ngfor-biglist/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { createGlobPatternsForDependencies } = require('@nx/angular/tailwind');
const { join } = require('path');

/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
join(__dirname, 'src/**/!(*.stories|*.spec).{ts,html}'),
...createGlobPatternsForDependencies(__dirname),
],
theme: {
extend: {},
},
plugins: [],
};
10 changes: 10 additions & 0 deletions apps/performance/ngfor-biglist/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
}
7 changes: 7 additions & 0 deletions apps/performance/ngfor-biglist/tsconfig.editor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"types": []
}
}
29 changes: 29 additions & 0 deletions apps/performance/ngfor-biglist/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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.app.json"
},
{
"path": "./tsconfig.editor.json"
}
],
"extends": "../../../tsconfig.base.json",
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
4 changes: 2 additions & 2 deletions challenge-number.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"total": 36,
"total": 37,
"🟢": 13,
"🟠": 116,
"🟠": 117,
"🔴": 207
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 🟢 Memoization
description: Challenge 35 is about learning
description: Challenge 35 is about learning how pure pipe works
sidebar:
order: 8
---
Expand Down
Loading

0 comments on commit cdf6df0

Please sign in to comment.