javascript / expert
Snippet
Fine-Grained Signal Equality Logic for Deep Object Comparison
By default, Angular signals use referential equality (===). For expert-level state management involving complex objects or immutable trees, providing a custom equality function in the signal options prevents unnecessary downstream computations and template re-renders when the data structure's content is logically identical despite a new reference.
snippet.js
1
2
3
const userProfile = signal({ id: '001', data: { name: 'Markus', roles: ['admin'] } }, {equal: (a, b) => a.id === b.id && JSON.stringify(a.data) === JSON.stringify(b.data)});
angular
Breakdown
1
const userProfile = signal(..., {
Initializes a signal with an initial object and an options object.
2
equal: (a, b) => ...
Defines a custom comparator function to determine if the signal value has truly changed.