Skip to content
Myr

shallow.md

This file

Path

docs/reference/apis/shallow.md

248 lines · 6.9 KiB · Markdown

Last changed by

Hotfix section linking (#3410)Copilotbefore adoptionc49df38

History of this file

Take it

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 1–18why, one step back
1---
2title: shallow3description: How compare simple data effectively4nav: 245---67`shallow` lets you run fast checks on simple data structures. It effectively identifies changes in8**top-level** properties when you're working with data structures that don't have nested objects or9arrays within them.1011> [!NOTE]12> Shallow lets you perform quick comparisons, but keep its limitations in mind.1314```js15const equal = shallow(a, b)16```1718- [Types](#types)

Hotfix section linking (#3410)Copilotbefore adoptionc49df38

line 19why, one step back
19 - [Signature](#signature)

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 20–26why, one step back
20- [Reference](#reference)
21- [Usage](#usage)22 - [Comparing Primitives](#comparing-primitives)23 - [Comparing Objects](#comparing-objects)24 - [Comparing Sets](#comparing-sets)25 - [Comparing Maps](#comparing-maps)26- [Troubleshooting](#troubleshooting)

Hotfix section linking (#3410)Copilotbefore adoptionc49df38

line 27why, one step back
27 - [Comparing objects returns `false` even if they are identical](#comparing-objects-returns-false-even-if-they-are-identical)

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 28–157why, one step back
28 - [Comparing objects with different prototypes](#comparing-objects-with-different-prototypes)
2930## Types3132### Signature3334```ts35shallow<T>(a: T, b: T): boolean36```3738## Reference3940### `shallow(a, b)`4142#### Parameters4344- `a`: The first value.45- `b`: The second value.4647#### Returns4849`shallow` returns `true` when `a` and `b` are equal based on a shallow comparison of their50**top-level** properties. Otherwise, it should return `false`.5152## Usage5354### Comparing Primitives5556When comparing primitive values like `string`s, `number`s, `boolean`s, and `BigInt`s, both57`Object.is` and `shallow` function return `true` if the values are the same. This is because58primitive values are compared by their actual value rather than by reference.5960```ts61const stringLeft = 'John Doe'62const stringRight = 'John Doe'6364Object.is(stringLeft, stringRight) // -> true65shallow(stringLeft, stringRight) // -> true6667const numberLeft = 1068const numberRight = 106970Object.is(numberLeft, numberRight) // -> true71shallow(numberLeft, numberRight) // -> true7273const booleanLeft = true74const booleanRight = true7576Object.is(booleanLeft, booleanRight) // -> true77shallow(booleanLeft, booleanRight) // -> true7879const bigIntLeft = 1n80const bigIntRight = 1n8182Object.is(bigIntLeft, bigIntRight) // -> true83shallow(bigIntLeft, bigIntRight) // -> true84```8586### Comparing Objects8788When comparing objects, it's important to understand how `Object.is` and `shallow` function89operate, as they handle comparisons differently.9091The `shallow` function returns `true` because shallow performs a shallow comparison of the objects.92It checks if the top-level properties and their values are the same. In this case, the top-level93properties (`firstName`, `lastName`, and `age`) and their values are identical between `objectLeft`94and `objectRight`, so shallow considers them equal.9596```ts97const objectLeft = {98 firstName: 'John',99 lastName: 'Doe',100 age: 30,101}102const objectRight = {103 firstName: 'John',104 lastName: 'Doe',105 age: 30,106}107108Object.is(objectLeft, objectRight) // -> false109shallow(objectLeft, objectRight) // -> true110```111112### Comparing Sets113114When comparing sets, it's important to understand how `Object.is` and `shallow` function operate,115as they handle comparisons differently.116117The `shallow` function returns `true` because shallow performs a shallow comparison of the sets. It118checks if the top-level properties (in this case, the sets themselves) are the same. Since `setLeft`119and `setRight` are both instances of the Set object and contain the same elements, shallow considers120them equal.121122```ts123const setLeft = new Set([1, 2, 3])124const setRight = new Set([1, 2, 3])125126Object.is(setLeft, setRight) // -> false127shallow(setLeft, setRight) // -> true128```129130### Comparing Maps131132When comparing maps, it's important to understand how `Object.is` and `shallow` function operate, as133they handle comparisons differently.134135The `shallow` returns `true` because shallow performs a shallow comparison of the maps. It checks if136the top-level properties (in this case, the maps themselves) are the same. Since `mapLeft` and137`mapRight` are both instances of the Map object and contain the same key-value pairs, shallow138considers them equal.139140```ts141const mapLeft = new Map([142 [1, 'one'],143 [2, 'two'],144 [3, 'three'],145])146const mapRight = new Map([147 [1, 'one'],148 [2, 'two'],149 [3, 'three'],150])151152Object.is(mapLeft, mapRight) // -> false153shallow(mapLeft, mapRight) // -> true154```155156## Troubleshooting157

Hotfix section linking (#3410)Copilotbefore adoptionc49df38

line 158why, one step back
158### Comparing objects returns `false` even if they are identical

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 159–248why, one step back
159
160The `shallow` function performs a shallow comparison. A shallow comparison checks if the top-level161properties of two objects are equal. It does not check nested objects or deeply nested properties.162In other words, it only compares the references of the properties.163164In the following example, the shallow function returns `false` because it compares only the165top-level properties and their references. The address property in both objects is a nested object,166and even though their contents are identical, their references are different. Consequently, shallow167sees them as different, resulting in `false`.168169```ts170const objectLeft = {171 firstName: 'John',172 lastName: 'Doe',173 age: 30,174 address: {175 street: 'Kulas Light',176 suite: 'Apt. 556',177 city: 'Gwenborough',178 zipcode: '92998-3874',179 geo: {180 lat: '-37.3159',181 lng: '81.1496',182 },183 },184}185const objectRight = {186 firstName: 'John',187 lastName: 'Doe',188 age: 30,189 address: {190 street: 'Kulas Light',191 suite: 'Apt. 556',192 city: 'Gwenborough',193 zipcode: '92998-3874',194 geo: {195 lat: '-37.3159',196 lng: '81.1496',197 },198 },199}200201Object.is(objectLeft, objectRight) // -> false202shallow(objectLeft, objectRight) // -> false203```204205If we remove the `address` property, the shallow comparison would work as expected because all206top-level properties would be primitive values or references to the same values:207208```ts209const objectLeft = {210 firstName: 'John',211 lastName: 'Doe',212 age: 30,213}214const objectRight = {215 firstName: 'John',216 lastName: 'Doe',217 age: 30,218}219220Object.is(objectLeft, objectRight) // -> false221shallow(objectLeft, objectRight) // -> true222```223224In this modified example, `objectLeft` and `objectRight` have the same top-level properties and225primitive values. Since `shallow` function only compares the top-level properties, it will return226`true` because the primitive values (`firstName`, `lastName`, and `age`) are identical in both227objects.228229### Comparing objects with different prototypes230231The `shallow` function checks whether the two objects have the same prototype. If their prototypes232are referentially different, shallow will return `false`. This comparison is done using:233234```ts235Object.getPrototypeOf(a) === Object.getPrototypeOf(b)236```237238> [!IMPORTANT]239> Objects created with the object initializer (`{}`) or with `new Object()` inherit from240> `Object.prototype` by default. However, objects created with `Object.create(proto)` inherit from241> the proto you pass in—which may not be `Object.prototype.`242243```ts244const a = Object.create({}) // -> prototype is `{}`245const b = {} // -> prototype is `Object.prototype`246247shallow(a, b) // -> false248```