Reference
shallow
useStore to prevent unnecessary reactivity updates.
T
The first value to compare.
T
The second value to compare.
Returns
boolean
true if the values are shallowly equal, false otherwise.Comparison Behavior
Theshallow function handles various data types:
- Reference equality: Returns
trueifObject.is(objA, objB) - Primitives/null: Returns
falseif either value is not an object - Map: Compares size and all entries using
Object.is - Set: Compares size and all values for presence
- Date: Compares timestamps using
getTime() - Objects: Compares own properties using
Object.is
Usage
Default Behavior
Explicit Usage
Reference Equality
Selecting Object Slices
Array Filtering
With Derived Values
Custom Comparison
Notes
- Used as the default equality function in
useStore - Only compares own properties at the first level (shallow)
- For nested objects, only the reference is compared
- Handles special object types: Map, Set, Date
- Optimized for Svelte 5’s runes-based reactivity
- Prevents unnecessary
$stateupdates when object contents are equivalent - More efficient than deep equality for most reactive scenarios