Sub topic 9
Custom equality functions
This chapter assumes you know how signal(), computed() and effect() work, and that you understand why computed values are lazy and memoized. You should also be comfortable reading TypeScript generics.
Why equality matters
A signal tells its consumers that it changed only when its new value is different from the old one. By default, "different" means Object.is(previous, next). For numbers, strings and booleans this works well. For objects and arrays it compares references, not contents.
That causes a common problem. Methods such as filter(), map() and slice() return a new array every time. A computed() that uses them produces a new reference on every recalculation, even when the contents are identical. Every consumer of that computed value then recalculates too.
A computed signal with an equal function
The second argument to computed() is an options object. Its equal property takes a function that receives the previous and next values and returns true when they should count as the same.
import { signal, computed } from '@angular/core';
interface Item { id: number; done: boolean; }
const items = signal<Item[]>([
{ id: 1, done: true },
{ id: 2, done: false },
]);
const doneItems = computed(() => items().filter((i) => i.done), {
equal: (a, b) => a.length === b.length && a.every((v, i) => v === b[i]),
});
let summaryRuns = 0;
const summary = computed(() => {
summaryRuns++;
return `${doneItems().length} done`;
});
console.log(summary(), summaryRuns); // '1 done' 1
// Add an unfinished item. doneItems recalculates, but its contents are unchanged.
items.update((list) => [...list, { id: 3, done: false }]);
console.log(summary(), summaryRuns); // '1 done' 1
// Add a finished item. The contents now differ, so summary recalculates.
items.update((list) => [...list, { id: 4, done: true }]);
console.log(summary(), summaryRuns); // '2 done' 2After the second update, doneItems ran its filter() and produced a new array. The equal function reported it as the same as the old one, so Angular kept the old array and did not mark summary as stale. Without equal, summaryRuns would be 2 after that update.
Equality on writable signals
signal() accepts the same option. This is useful for object values where only some fields matter.
import { signal } from '@angular/core';
interface Point { x: number; y: number; }
const position = signal<Point>(
{ x: 0, y: 0 },
{ equal: (a, b) => a.x === b.x && a.y === b.y },
);
position.set({ x: 0, y: 0 }); // treated as equal, so nothing is notified
position.set({ x: 5, y: 0 }); // different, so consumers updateCalling set() with a value that your equal function considers equal is a no-op. The signal keeps its old value and consumers are not notified.
Choosing an equal function
| Situation | Suitable `equal` |
|---|---|
| Array of primitives or stable object references | Same length and `every` element `===` |
| Object with a few relevant fields | Compare only those fields |
| Large nested data | Compare an ID, a version number or a timestamp instead of walking the structure |
| Values already primitive | Leave the default |
Keep the function cheap and free of side effects. Angular calls it on every recalculation, and a slow comparison can cost more than the work it saves.
Common errors
- Mutating in place. If you push into an array and then call
set()with the same array, the default check sees the same reference and notifies nobody. Create a new array withupdate((list) => [...list, item]). - Comparing too deeply. A recursive deep-equal on a large structure runs on every recalculation. Prefer comparing a key or version field.
- Forgetting that the old value is kept. When
equalreturnstrue, consumers keep seeing the previous object. Do not rely on the new object's identity. - Using
equalas a fix for wrong dependencies. If a computed recalculates too often because it reads signals it does not need, fix the reads. Useuntracked()for those reads, as covered in the previous chapter.equalonly limits how far the change spreads. It does not stop the computed from running.
Exercise
A signal filters = signal({ text: '', page: 1 }) should notify consumers only when text changes, never when only page changes. Write the options object for signal() that does this, and say one risk of this approach.
Show answer
Use signal({ text: '', page: 1 }, { equal: (a, b) => a.text === b.text }). Calling set() with a different page but the same text is then ignored, so the signal keeps the old page value. The risk is that code reading filters().page can see a stale page number, because the update was discarded. Keeping page in its own signal avoids this.