javascript / expert
Snippet
Fine-grained Update Control with Computed Equality Functions
Signals in Angular allow for custom equality functions in 'computed' and 'signal' definitions. By overriding the default identity check, you can prevent unnecessary downstream recomputations when complex objects change but only specific properties matter for the derived state.
snippet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
export class UserProfile {user = signal({ id: 1, name: 'Max', lastLogin: new Date() });// Only recompute if the ID changes, ignoring lastLogin updatesuserId = computed(() => this.user().id,{ equal: (a, b) => a === b });constructor() {effect(() => console.log('User ID updated:', this.userId()));}}
angular
Breakdown
1
computed(() => this.user().id, { equal: ... })
Defines a derived signal with a custom comparison strategy.
2
equal: (a, b) => a === b
Explicitly tells the signal to only notify consumers if the primitive value differs.
3
effect(() => ...)
This block will only trigger if the 'equal' check returns false.