Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Create avoid heavy computations in getters #142

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions content/components/avoid heavy computations in getters
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: avoid heavy computations in getters
author:
name: Claude Assistant
url: https://github.com/claude
---

# Problem
Using getters with complex computations or transformations in components can lead to performance issues since getters are executed on every change detection cycle, even if the underlying data hasn't changed. This can cause unnecessary re-calculations and slow down the application.

# Solution
Instead of using getters for heavy computations, use regular methods or calculate values when the data actually changes (e.g., in ngOnChanges or when triggered by specific actions).

- **Bad practice**
```ts
@Component({
template: `
<div>
<h3>Product Statistics</h3>
<p>Filtered Products: {{ filteredProducts.length }}</p>
<ul>
<li *ngFor="let product of filteredProducts">
{{ product.name }} - {{ product.price }}
</li>
</ul>
</div>
`
})
export class ProductsComponent {
private _products: Product[] = [];

// Getter will run on every change detection
get filteredProducts() {
return this._products
.filter(p => p.isActive)
.sort((a, b) => b.price - a.price)
.map(p => ({
...p,
name: p.name.toUpperCase(),
price: this.calculateDiscountedPrice(p)
}));
}

private calculateDiscountedPrice(product: Product): number {
// Complex price calculation
return product.price * (1 - product.discount) * this.seasonalMultiplier;
}
}

Good practice

tsCopy@Component({
template: `
<div>
<h3>Product Statistics</h3>
<p>Filtered Products: {{ filteredProducts.length }}</p>
<ul>
<li *ngFor="let product of filteredProducts">
{{ product.name }} - {{ product.price }}
</li>
</ul>
</div>
`
})
export class ProductsComponent implements OnInit, OnChanges {
private _products: Product[] = [];
filteredProducts: ProcessedProduct[] = [];

@Input() set products(value: Product[]) {
this._products = value;
this.updateFilteredProducts();
}

ngOnInit() {
this.updateFilteredProducts();
}

ngOnChanges(changes: SimpleChanges) {
if (changes['products']) {
this.updateFilteredProducts();
}
}

private updateFilteredProducts() {
this.filteredProducts = this._products
.filter(p => p.isActive)
.sort((a, b) => b.price - a.price)
.map(p => ({
...p,
name: p.name.toUpperCase(),
price: this.calculateDiscountedPrice(p)
}));
}

private calculateDiscountedPrice(product: Product): number {
return product.price * (1 - product.discount) * this.seasonalMultiplier;
}
}
Resources

Angular Performance Best Practices
Change Detection in Angular
Using OnPush Change Detection Strategy