mirror of
https://github.com/angular/angular.git
synced 2026-11-22 22:55:25 -05:00
docs: add guidance against expensive work in templates
AI coding agents using the angular-developer skill were observed calling
plain methods inside @for/@if template expressions, for example
items(count) returning a new array on every check. The skill had no
guidance on this.
Add a short rule to SKILL.md and a section to components.md that
recommends computed(), a pure pipe or a child component for expensive
work that would otherwise re-run on every change detection cycle. Signal
reads and cheap calls stay allowed.
Related to #70132
(cherry picked from commit 09264663af)
This commit is contained in:
committed by
Alon Mishne
parent
98049217e5
commit
5ef157446a
@@ -54,6 +54,8 @@ When working with Angular components, consult the following references based on
|
||||
- **Deferrable Views**: Lazy loading, code splitting, triggers, prefetching, loading states, SSR, and layout stability with `@defer`. Read [deferrable-views.md](references/deferrable-views.md)
|
||||
- **Error Boundaries**: Catching render and change detection errors in a template subtree with `@boundary`/`@error`, retrying with `$reset`. Read [error-boundary.md](references/error-boundary.md)
|
||||
|
||||
**Template expressions**: Avoid expensive work in templates (e.g. building arrays or objects, filtering, sorting), since it re-runs on every change detection cycle. Reading signals (`count()`) and cheap calls are fine. Derive other values with `computed()`, a pure pipe, or a child component. See [Avoiding expensive work in templates](references/components.md#avoiding-expensive-work-in-templates).
|
||||
|
||||
If you require deeper documentation not found in the references above, read the documentation at `https://angular.dev/guide/components`.
|
||||
|
||||
## Reactivity and Data Management
|
||||
|
||||
@@ -108,6 +108,16 @@ The `@for` block iterates over collections. The `track` expression is **required
|
||||
|
||||
**Implicit Variables**: `$index`, `$count`, `$first`, `$last`, `$even`, `$odd`.
|
||||
|
||||
### Avoiding expensive work in templates
|
||||
|
||||
Template expressions are re-evaluated every time the component's template is checked, so avoid doing expensive work in them (e.g. building or filtering arrays, sorting, heavy formatting). Calling a cheap method is fine; the concern is expensive work repeated on every change detection cycle. A method that returns a new array or object also hands `@for` a new collection on every run.
|
||||
|
||||
Reading a signal (`count()`, `items()`) is fine, and so is a trivial lookup. For anything expensive, derive the value once:
|
||||
|
||||
- **`computed()`** for values derived from signals.
|
||||
- **A pure pipe** for formatting a value (see [pipes.md](pipes.md)).
|
||||
- **A child component** when each item needs its own derived state.
|
||||
|
||||
### Switching Content (`@switch`)
|
||||
|
||||
The `@switch` block renders content based on a value. It uses strict equality (`===`) and has **no fallthrough**.
|
||||
|
||||
Reference in New Issue
Block a user