fast-equals 6.1.1 → 6.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -33
- package/dist/cjs/index.cjs +189 -95
- package/dist/cjs/index.d.cts +2 -1
- package/dist/es/index.d.mts +2 -1
- package/dist/es/index.mjs +189 -95
- package/index.d.ts +2 -1
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
> fast-equals
|
|
2
2
|
|
|
3
3
|
Perform [blazing fast](#benchmarks) equality comparisons between two objects, while also allowing for flexibility for
|
|
4
|
-
various use-cases. It has no dependencies, and is ~2.
|
|
4
|
+
various use-cases. It has no dependencies, and is ~2.65kB when minified and gzipped.
|
|
5
5
|
|
|
6
6
|
The following types are handled out-of-the-box:
|
|
7
7
|
|
|
@@ -235,6 +235,7 @@ Performs the same comparison as `deepEqual` but performs a strict comparison of
|
|
|
235
235
|
- Checking full descriptor of properties on the object to match
|
|
236
236
|
- Checking non-index properties on arrays
|
|
237
237
|
- Checking non-key properties on `Map` / `Set` objects
|
|
238
|
+
- Checking own properties on `Date` objects
|
|
238
239
|
|
|
239
240
|
```ts
|
|
240
241
|
const array = [{ foo: 'bar' }];
|
|
@@ -255,6 +256,7 @@ Performs the same comparison as `shallowEqual` but performs a strict comparison
|
|
|
255
256
|
- Checking full descriptor of properties on the object to match
|
|
256
257
|
- Checking non-index properties on arrays
|
|
257
258
|
- Checking non-key properties on `Map` / `Set` objects
|
|
259
|
+
- Checking own properties on `Date` objects
|
|
258
260
|
|
|
259
261
|
```ts
|
|
260
262
|
const array = ['foo'];
|
|
@@ -276,6 +278,7 @@ Performs the same comparison as `circularDeepEqual` but performs a strict compar
|
|
|
276
278
|
- Checking full descriptor of properties on the object to match
|
|
277
279
|
- Checking non-index properties on arrays
|
|
278
280
|
- Checking non-key properties on `Map` / `Set` objects
|
|
281
|
+
- Checking own properties on `Date` objects
|
|
279
282
|
|
|
280
283
|
```ts
|
|
281
284
|
function Circular(value) {
|
|
@@ -315,6 +318,7 @@ Performs the same comparison as `circularShallowEqual` but performs a strict com
|
|
|
315
318
|
- Checking full descriptor of properties on the object to match
|
|
316
319
|
- Checking non-index properties on arrays
|
|
317
320
|
- Checking non-key properties on `Map` / `Set` objects
|
|
321
|
+
- Checking own properties on `Date` objects
|
|
318
322
|
|
|
319
323
|
```ts
|
|
320
324
|
const array = ['foo'];
|
|
@@ -441,74 +445,74 @@ Testing mixed objects equal...
|
|
|
441
445
|
┌────────────────────────────────────────┬────────────────┐
|
|
442
446
|
│ Name │ Ops / sec │
|
|
443
447
|
├────────────────────────────────────────┼────────────────┤
|
|
444
|
-
│ fast-equals (passed) │
|
|
448
|
+
│ fast-equals (passed) │ 1763109.81851 │
|
|
445
449
|
├────────────────────────────────────────┼────────────────┤
|
|
446
|
-
│
|
|
450
|
+
│ fast-deep-equal (passed) │ 1297247.799465 │
|
|
447
451
|
├────────────────────────────────────────┼────────────────┤
|
|
448
|
-
│ fast-
|
|
452
|
+
│ react-fast-compare (passed) │ 1271146.258704 │
|
|
449
453
|
├────────────────────────────────────────┼────────────────┤
|
|
450
|
-
│ shallow-equal-fuzzy (passed) │
|
|
454
|
+
│ shallow-equal-fuzzy (passed) │ 1235115.693854 │
|
|
451
455
|
├────────────────────────────────────────┼────────────────┤
|
|
452
|
-
│ nano-equal (failed) │
|
|
456
|
+
│ nano-equal (failed) │ 964815.367752 │
|
|
453
457
|
├────────────────────────────────────────┼────────────────┤
|
|
454
|
-
│ fast-equals (circular) (passed) │
|
|
458
|
+
│ fast-equals (circular) (passed) │ 834232.686567 │
|
|
455
459
|
├────────────────────────────────────────┼────────────────┤
|
|
456
|
-
│ dequal
|
|
460
|
+
│ dequal (passed) │ 759417.072806 │
|
|
457
461
|
├────────────────────────────────────────┼────────────────┤
|
|
458
|
-
│ dequal (passed)
|
|
462
|
+
│ dequal/lite (passed) │ 751901.517376 │
|
|
459
463
|
├────────────────────────────────────────┼────────────────┤
|
|
460
|
-
│ underscore.isEqual (passed) │
|
|
464
|
+
│ underscore.isEqual (passed) │ 447789.934941 │
|
|
461
465
|
├────────────────────────────────────────┼────────────────┤
|
|
462
|
-
│ assert.deepStrictEqual (passed) │
|
|
466
|
+
│ assert.deepStrictEqual (passed) │ 433256.455121 │
|
|
463
467
|
├────────────────────────────────────────┼────────────────┤
|
|
464
|
-
│ es-toolkit (passed) │
|
|
468
|
+
│ es-toolkit (passed) │ 343382.909087 │
|
|
465
469
|
├────────────────────────────────────────┼────────────────┤
|
|
466
|
-
│ fast-equals (strict) (passed) │
|
|
470
|
+
│ fast-equals (strict) (passed) │ 294211.792998 │
|
|
467
471
|
├────────────────────────────────────────┼────────────────┤
|
|
468
|
-
│ lodash.isEqual (passed) │
|
|
472
|
+
│ lodash.isEqual (passed) │ 290477.460766 │
|
|
469
473
|
├────────────────────────────────────────┼────────────────┤
|
|
470
|
-
│ fast-equals (strict circular) (passed) │
|
|
474
|
+
│ fast-equals (strict circular) (passed) │ 232110.748904 │
|
|
471
475
|
├────────────────────────────────────────┼────────────────┤
|
|
472
|
-
│ deep-eql (passed) │
|
|
476
|
+
│ deep-eql (passed) │ 161408.4511 │
|
|
473
477
|
├────────────────────────────────────────┼────────────────┤
|
|
474
|
-
│ deep-equal (passed) │
|
|
478
|
+
│ deep-equal (passed) │ 951.279346 │
|
|
475
479
|
└────────────────────────────────────────┴────────────────┘
|
|
476
480
|
|
|
477
481
|
Testing mixed objects not equal...
|
|
478
482
|
┌────────────────────────────────────────┬────────────────┐
|
|
479
483
|
│ Name │ Ops / sec │
|
|
480
484
|
├────────────────────────────────────────┼────────────────┤
|
|
481
|
-
│ fast-equals (passed) │
|
|
485
|
+
│ fast-equals (passed) │ 5402008.064431 │
|
|
482
486
|
├────────────────────────────────────────┼────────────────┤
|
|
483
|
-
│ fast-
|
|
487
|
+
│ fast-equals (circular) (passed) │ 3687783.563818 │
|
|
484
488
|
├────────────────────────────────────────┼────────────────┤
|
|
485
|
-
│ fast-
|
|
489
|
+
│ fast-deep-equal (passed) │ 3479884.479164 │
|
|
486
490
|
├────────────────────────────────────────┼────────────────┤
|
|
487
|
-
│ react-fast-compare (passed) │
|
|
491
|
+
│ react-fast-compare (passed) │ 3356141.586592 │
|
|
488
492
|
├────────────────────────────────────────┼────────────────┤
|
|
489
|
-
│ fast-equals (strict) (passed) │
|
|
493
|
+
│ fast-equals (strict) (passed) │ 2927133.435788 │
|
|
490
494
|
├────────────────────────────────────────┼────────────────┤
|
|
491
|
-
│ fast-equals (strict circular) (passed) │
|
|
495
|
+
│ fast-equals (strict circular) (passed) │ 1566187.833636 │
|
|
492
496
|
├────────────────────────────────────────┼────────────────┤
|
|
493
|
-
│ dequal/lite (passed) │
|
|
497
|
+
│ dequal/lite (passed) │ 1316031.262808 │
|
|
494
498
|
├────────────────────────────────────────┼────────────────┤
|
|
495
|
-
│ dequal (passed) │
|
|
499
|
+
│ dequal (passed) │ 1298255.762153 │
|
|
496
500
|
├────────────────────────────────────────┼────────────────┤
|
|
497
|
-
│ shallow-equal-fuzzy (failed) │
|
|
501
|
+
│ shallow-equal-fuzzy (failed) │ 1219217.578795 │
|
|
498
502
|
├────────────────────────────────────────┼────────────────┤
|
|
499
|
-
│ nano-equal (passed) │
|
|
503
|
+
│ nano-equal (passed) │ 1074115.593615 │
|
|
500
504
|
├────────────────────────────────────────┼────────────────┤
|
|
501
|
-
│ underscore.isEqual (passed) │
|
|
505
|
+
│ underscore.isEqual (passed) │ 659016.868085 │
|
|
502
506
|
├────────────────────────────────────────┼────────────────┤
|
|
503
|
-
│ lodash.isEqual (passed) │
|
|
507
|
+
│ lodash.isEqual (passed) │ 374944.853605 │
|
|
504
508
|
├────────────────────────────────────────┼────────────────┤
|
|
505
|
-
│ es-toolkit (passed) │
|
|
509
|
+
│ es-toolkit (passed) │ 355882.255615 │
|
|
506
510
|
├────────────────────────────────────────┼────────────────┤
|
|
507
|
-
│ deep-eql (passed) │
|
|
511
|
+
│ deep-eql (passed) │ 182235.73069 │
|
|
508
512
|
├────────────────────────────────────────┼────────────────┤
|
|
509
|
-
│ assert.deepStrictEqual (passed) │
|
|
513
|
+
│ assert.deepStrictEqual (passed) │ 20975.582116 │
|
|
510
514
|
├────────────────────────────────────────┼────────────────┤
|
|
511
|
-
│ deep-equal (passed) │
|
|
515
|
+
│ deep-equal (passed) │ 3760.574487 │
|
|
512
516
|
└────────────────────────────────────────┴────────────────┘
|
|
513
517
|
```
|
|
514
518
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,8 +1,40 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
|
|
3
|
+
const { getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols } = Object;
|
|
4
4
|
const { hasOwnProperty, toString: toString$1 } = Object.prototype;
|
|
5
5
|
const OBJECT_TAG = '[object Object]';
|
|
6
|
+
/**
|
|
7
|
+
* Whether the own properties from `end` onward are equal with strict property checking, where the
|
|
8
|
+
* caller has already verified that both objects have the same number of own properties.
|
|
9
|
+
*/
|
|
10
|
+
function areStrictPropertiesEqual(a, b, state, properties, end) {
|
|
11
|
+
let index = properties.length;
|
|
12
|
+
let property;
|
|
13
|
+
let descriptorA;
|
|
14
|
+
let descriptorB;
|
|
15
|
+
// Decrementing `while` showed faster results than either incrementing or
|
|
16
|
+
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
17
|
+
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
18
|
+
// cost of anonymous callbacks.
|
|
19
|
+
while (index-- > end) {
|
|
20
|
+
property = properties[index];
|
|
21
|
+
if (!isReactProperty(a, b, property)
|
|
22
|
+
&& (!hasOwn(b, property) || !state.equals(a[property], b[property], property, property, a, b, state))) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
descriptorA = getOwnPropertyDescriptor(a, property);
|
|
26
|
+
descriptorB = getOwnPropertyDescriptor(b, property);
|
|
27
|
+
if ((descriptorA || descriptorB)
|
|
28
|
+
&& (!descriptorA
|
|
29
|
+
|| !descriptorB
|
|
30
|
+
|| descriptorA.configurable !== descriptorB.configurable
|
|
31
|
+
|| descriptorA.enumerable !== descriptorB.enumerable
|
|
32
|
+
|| descriptorA.writable !== descriptorB.writable)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
6
38
|
/**
|
|
7
39
|
* Combine two comparators into a single comparators.
|
|
8
40
|
*/
|
|
@@ -45,6 +77,10 @@ function getStrictProperties(object) {
|
|
|
45
77
|
? getOwnPropertyNames(object).concat(symbols)
|
|
46
78
|
: getOwnPropertyNames(object);
|
|
47
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Identify the element type of TypedArrays across realms and subclasses.
|
|
82
|
+
*/
|
|
83
|
+
const getTypedArrayTag = getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
|
|
48
84
|
/**
|
|
49
85
|
* Whether the object contains the property passed as an own property.
|
|
50
86
|
*/
|
|
@@ -67,15 +103,37 @@ function isNullPrototypeComparable(a, b) {
|
|
|
67
103
|
}
|
|
68
104
|
return false;
|
|
69
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Whether the property passed is a property specific to React/Preact elements. These are ignored in comparison, as they
|
|
108
|
+
* are deeply circular and will return frequent false negatives for the comparison that matters, which is often the
|
|
109
|
+
* React element's aspects (`props`, `type`, etc.).
|
|
110
|
+
*/
|
|
111
|
+
function isReactProperty(a, b, property) {
|
|
112
|
+
return (
|
|
113
|
+
// _owner -> React owner
|
|
114
|
+
// __o -> Preact owner
|
|
115
|
+
// __v -> Preact vnode
|
|
116
|
+
(property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Reorder a serialized query string so that params holding the same pairs compare as equal
|
|
120
|
+
* regardless of the order they appear in.
|
|
121
|
+
*
|
|
122
|
+
* @note
|
|
123
|
+
* The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
|
|
124
|
+
* splitting on `&` recovers exactly the pairs and nothing else.
|
|
125
|
+
*/
|
|
126
|
+
function sortSearchParams(serialized) {
|
|
127
|
+
return serialized.split('&').sort().join('&');
|
|
128
|
+
}
|
|
70
129
|
|
|
71
|
-
const PREACT_VNODE = '__v';
|
|
72
|
-
const PREACT_OWNER = '__o';
|
|
73
|
-
const REACT_OWNER = '_owner';
|
|
74
|
-
const { getOwnPropertyDescriptor, keys } = Object;
|
|
75
130
|
/**
|
|
76
|
-
*
|
|
131
|
+
* Above this many keys, out-of-position keys are checked with `propertyIsEnumerable` rather than by
|
|
132
|
+
* scanning the other object's keys. Measured in V8: scanning wins up to 50 keys and loses from 100.
|
|
77
133
|
*/
|
|
78
|
-
const
|
|
134
|
+
const MAX_SCANNED_PROPERTIES = 64;
|
|
135
|
+
const { keys } = Object;
|
|
136
|
+
const { propertyIsEnumerable } = Object.prototype;
|
|
79
137
|
/**
|
|
80
138
|
* Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
|
|
81
139
|
* Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
|
|
@@ -149,16 +207,68 @@ function areDatesEqual(a, b) {
|
|
|
149
207
|
*
|
|
150
208
|
* @note
|
|
151
209
|
* `name`, `message` and `stack` are own properties but are not enumerable, so they are compared
|
|
152
|
-
* explicitly. `cause`
|
|
153
|
-
* nested value in the comparison is treated.
|
|
154
|
-
*
|
|
155
|
-
*
|
|
210
|
+
* explicitly. `cause` and `errors` are compared by value rather than by reference, matching how
|
|
211
|
+
* every other nested value in the comparison is treated. They are compared exactly once, regardless
|
|
212
|
+
* of whether they were assigned (enumerable) or set by the constructor (non-enumerable). Other own
|
|
213
|
+
* enumerable properties (which custom `Error` subclasses commonly add) are compared afterward.
|
|
214
|
+
*
|
|
215
|
+
* The own property comparison is inlined rather than extracted, which measured faster in V8 when
|
|
216
|
+
* recursing through `cause`. Keep the bytecode under V8's 460-byte TurboFan inlining limit (383 bytes
|
|
217
|
+
* at time of writing; check with `node --print-bytecode --print-bytecode-filter=areErrorsEqual`), or
|
|
218
|
+
* this stops being inlined into its callers.
|
|
156
219
|
*/
|
|
157
220
|
function areErrorsEqual(a, b, state) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
221
|
+
if (a.name !== b.name || a.message !== b.message || a.stack !== b.stack) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
const causeA = a.cause;
|
|
225
|
+
const causeB = b.cause;
|
|
226
|
+
if (causeA !== causeB && !state.equals(causeA, causeB, 'cause', 'cause', a, b, state)) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
const errorsA = a.errors;
|
|
230
|
+
const errorsB = b.errors;
|
|
231
|
+
if (errorsA !== errorsB && !state.equals(errorsA, errorsB, 'errors', 'errors', a, b, state)) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
const propertiesA = keys(a);
|
|
235
|
+
const propertiesB = keys(b);
|
|
236
|
+
let index = propertiesA.length;
|
|
237
|
+
// Constructed errors rarely have enumerable own properties.
|
|
238
|
+
if (!index && !propertiesB.length) {
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
let compared = 0;
|
|
242
|
+
let property;
|
|
243
|
+
while (index-- > 0) {
|
|
244
|
+
property = propertiesA[index];
|
|
245
|
+
// Already compared above regardless of enumerability, so we can ignore them.
|
|
246
|
+
if (property === 'cause' || property === 'errors') {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
// `propertyIsEnumerable` rather than `hasOwn`, so that a non-enumerable own property on `b` cannot
|
|
250
|
+
// stand in for an enumerable one. Keys in the same position are trivially enumerable on `b`.
|
|
251
|
+
if ((property !== propertiesB[index] && !propertyIsEnumerable.call(b, property))
|
|
252
|
+
|| !state.equals(a[property], b[property], property, property, a, b, state)) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
++compared;
|
|
256
|
+
}
|
|
257
|
+
// Every compared key is enumerable on `b`, so any of `b`'s keys left over must be `cause` / `errors`.
|
|
258
|
+
const remaining = propertiesB.length - compared;
|
|
259
|
+
return (!remaining
|
|
260
|
+
|| remaining === (propertyIsEnumerable.call(b, 'cause') ? 1 : 0) + (propertyIsEnumerable.call(b, 'errors') ? 1 : 0));
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Whether the errors passed are equal in value.
|
|
264
|
+
*
|
|
265
|
+
* @note
|
|
266
|
+
* `name`, `message` and `stack` are own properties but are not enumerable, so they are compared
|
|
267
|
+
* explicitly. `cause` and `errors` are own properties, so `areObjectsEqualStrict` compares them along
|
|
268
|
+
* with every other own property; they are not compared explicitly to avoid comparing them twice.
|
|
269
|
+
*/
|
|
270
|
+
function areErrorsEqualStrict(a, b, state) {
|
|
271
|
+
return a.name === b.name && a.message === b.message && a.stack === b.stack && areObjectsEqualStrict(a, b, state);
|
|
162
272
|
}
|
|
163
273
|
/**
|
|
164
274
|
* Whether the `Map`s are equal in value.
|
|
@@ -171,7 +281,7 @@ function areMapsEqual(a, b, state) {
|
|
|
171
281
|
if (!size) {
|
|
172
282
|
return true;
|
|
173
283
|
}
|
|
174
|
-
const matchedIndices = new
|
|
284
|
+
const matchedIndices = new Array(size);
|
|
175
285
|
const aIterable = a.entries();
|
|
176
286
|
let aResult;
|
|
177
287
|
let bResult;
|
|
@@ -182,7 +292,7 @@ function areMapsEqual(a, b, state) {
|
|
|
182
292
|
break;
|
|
183
293
|
}
|
|
184
294
|
const bIterable = b.entries();
|
|
185
|
-
let hasMatch =
|
|
295
|
+
let hasMatch = false;
|
|
186
296
|
let matchIndex = 0;
|
|
187
297
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
188
298
|
while ((bResult = bIterable.next())) {
|
|
@@ -197,7 +307,7 @@ function areMapsEqual(a, b, state) {
|
|
|
197
307
|
const bEntry = bResult.value;
|
|
198
308
|
if (state.equals(aEntry[0], bEntry[0], index, matchIndex, a, b, state)
|
|
199
309
|
&& state.equals(aEntry[1], bEntry[1], aEntry[0], bEntry[0], a, b, state)) {
|
|
200
|
-
hasMatch = matchedIndices[matchIndex] =
|
|
310
|
+
hasMatch = matchedIndices[matchIndex] = true;
|
|
201
311
|
break;
|
|
202
312
|
}
|
|
203
313
|
matchIndex++;
|
|
@@ -214,16 +324,30 @@ function areMapsEqual(a, b, state) {
|
|
|
214
324
|
*/
|
|
215
325
|
function areObjectsEqual(a, b, state) {
|
|
216
326
|
const properties = keys(a);
|
|
327
|
+
const propertiesB = keys(b);
|
|
217
328
|
let index = properties.length;
|
|
218
|
-
if (
|
|
329
|
+
if (propertiesB.length !== index) {
|
|
219
330
|
return false;
|
|
220
331
|
}
|
|
332
|
+
const isScannable = index <= MAX_SCANNED_PROPERTIES;
|
|
333
|
+
let property;
|
|
221
334
|
// Decrementing `while` showed faster results than either incrementing or
|
|
222
335
|
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
223
336
|
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
224
337
|
// cost of anonymous callbacks.
|
|
225
338
|
while (index-- > 0) {
|
|
226
|
-
|
|
339
|
+
property = properties[index];
|
|
340
|
+
if (isReactProperty(a, b, property)) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
// Objects built the same way list their keys in the same order, in which case the key is
|
|
344
|
+
// already known to be an own enumerable property of `b` and the lookup can be skipped.
|
|
345
|
+
// Otherwise, `propertiesB` is exactly `b`'s own enumerable keys. Scanning it is faster than
|
|
346
|
+
// `propertyIsEnumerable` for smaller objects, but it is linear per key, so larger objects
|
|
347
|
+
// (where reordering would make it quadratic) use the lookup instead.
|
|
348
|
+
if ((property !== propertiesB[index]
|
|
349
|
+
&& !(isScannable ? propertiesB.includes(property) : propertyIsEnumerable.call(b, property)))
|
|
350
|
+
|| !state.equals(a[property], b[property], property, property, a, b, state)) {
|
|
227
351
|
return false;
|
|
228
352
|
}
|
|
229
353
|
}
|
|
@@ -234,34 +358,7 @@ function areObjectsEqual(a, b, state) {
|
|
|
234
358
|
*/
|
|
235
359
|
function areObjectsEqualStrict(a, b, state) {
|
|
236
360
|
const properties = getStrictProperties(a);
|
|
237
|
-
|
|
238
|
-
if (getStrictProperties(b).length !== index) {
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
let property;
|
|
242
|
-
let descriptorA;
|
|
243
|
-
let descriptorB;
|
|
244
|
-
// Decrementing `while` showed faster results than either incrementing or
|
|
245
|
-
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
246
|
-
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
247
|
-
// cost of anonymous callbacks.
|
|
248
|
-
while (index-- > 0) {
|
|
249
|
-
property = properties[index];
|
|
250
|
-
if (!isPropertyEqual(a, b, state, property)) {
|
|
251
|
-
return false;
|
|
252
|
-
}
|
|
253
|
-
descriptorA = getOwnPropertyDescriptor(a, property);
|
|
254
|
-
descriptorB = getOwnPropertyDescriptor(b, property);
|
|
255
|
-
if ((descriptorA || descriptorB)
|
|
256
|
-
&& (!descriptorA
|
|
257
|
-
|| !descriptorB
|
|
258
|
-
|| descriptorA.configurable !== descriptorB.configurable
|
|
259
|
-
|| descriptorA.enumerable !== descriptorB.enumerable
|
|
260
|
-
|| descriptorA.writable !== descriptorB.writable)) {
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
return true;
|
|
361
|
+
return getStrictProperties(b).length === properties.length && areStrictPropertiesEqual(a, b, state, properties, 0);
|
|
265
362
|
}
|
|
266
363
|
/**
|
|
267
364
|
* Whether the primitive wrappers passed are equal in value.
|
|
@@ -275,6 +372,21 @@ function arePrimitiveWrappersEqual(a, b) {
|
|
|
275
372
|
function areRegExpsEqual(a, b) {
|
|
276
373
|
return a.source === b.source && a.flags === b.flags;
|
|
277
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Whether the search params passed are equal in value.
|
|
377
|
+
*
|
|
378
|
+
* @note
|
|
379
|
+
* Order is not significant, matching how the other unordered collections in the library are
|
|
380
|
+
* compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
|
|
381
|
+
* `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
|
|
382
|
+
*/
|
|
383
|
+
function areSearchParamsEqual(a, b) {
|
|
384
|
+
const serializedA = a.toString();
|
|
385
|
+
const serializedB = b.toString();
|
|
386
|
+
// Identical serializations are equal under any ordering, and this is by far the common case, so
|
|
387
|
+
// it is worth checking before sorting anything.
|
|
388
|
+
return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
|
|
389
|
+
}
|
|
278
390
|
/**
|
|
279
391
|
* Whether the `Set`s are equal in value.
|
|
280
392
|
*/
|
|
@@ -286,7 +398,7 @@ function areSetsEqual(a, b, state) {
|
|
|
286
398
|
if (!size) {
|
|
287
399
|
return true;
|
|
288
400
|
}
|
|
289
|
-
const matchedIndices = new
|
|
401
|
+
const matchedIndices = new Array(size);
|
|
290
402
|
const aIterable = a.values();
|
|
291
403
|
let aResult;
|
|
292
404
|
let bResult;
|
|
@@ -296,7 +408,7 @@ function areSetsEqual(a, b, state) {
|
|
|
296
408
|
break;
|
|
297
409
|
}
|
|
298
410
|
const bIterable = b.values();
|
|
299
|
-
let hasMatch =
|
|
411
|
+
let hasMatch = false;
|
|
300
412
|
let matchIndex = 0;
|
|
301
413
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
302
414
|
while ((bResult = bIterable.next())) {
|
|
@@ -305,7 +417,7 @@ function areSetsEqual(a, b, state) {
|
|
|
305
417
|
}
|
|
306
418
|
if (!matchedIndices[matchIndex]
|
|
307
419
|
&& state.equals(aResult.value, bResult.value, aResult.value, bResult.value, a, b, state)) {
|
|
308
|
-
hasMatch = matchedIndices[matchIndex] =
|
|
420
|
+
hasMatch = matchedIndices[matchIndex] = true;
|
|
309
421
|
break;
|
|
310
422
|
}
|
|
311
423
|
matchIndex++;
|
|
@@ -345,6 +457,21 @@ function areTypedArraysEqual(a, b) {
|
|
|
345
457
|
}
|
|
346
458
|
return true;
|
|
347
459
|
}
|
|
460
|
+
/**
|
|
461
|
+
* Whether the TypedArray instances are equal in value with strict property checking.
|
|
462
|
+
*
|
|
463
|
+
* @note
|
|
464
|
+
* Indexed elements are also own properties, listed first and in order, with descriptors fixed by the
|
|
465
|
+
* spec. They are compared once by `areTypedArraysEqual`, so only the own properties after them
|
|
466
|
+
* (including symbols) are compared with strict property checking.
|
|
467
|
+
*/
|
|
468
|
+
function areTypedArraysEqualStrict(a, b, state) {
|
|
469
|
+
if (!areTypedArraysEqual(a, b)) {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
const properties = getStrictProperties(a);
|
|
473
|
+
return (getStrictProperties(b).length === properties.length && areStrictPropertiesEqual(a, b, state, properties, a.length));
|
|
474
|
+
}
|
|
348
475
|
/**
|
|
349
476
|
* Whether the URL instances are equal in value.
|
|
350
477
|
*/
|
|
@@ -363,39 +490,6 @@ function areUrlsEqual(a, b) {
|
|
|
363
490
|
&& a.hash === b.hash
|
|
364
491
|
&& areSearchParamsEqual(a.searchParams, b.searchParams));
|
|
365
492
|
}
|
|
366
|
-
/**
|
|
367
|
-
* Whether the search params passed are equal in value.
|
|
368
|
-
*
|
|
369
|
-
* @note
|
|
370
|
-
* Order is not significant, matching how the other unordered collections in the library are
|
|
371
|
-
* compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
|
|
372
|
-
* `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
|
|
373
|
-
*/
|
|
374
|
-
function areSearchParamsEqual(a, b) {
|
|
375
|
-
const serializedA = a.toString();
|
|
376
|
-
const serializedB = b.toString();
|
|
377
|
-
// Identical serializations are equal under any ordering, and this is by far the common case, so
|
|
378
|
-
// it is worth checking before sorting anything.
|
|
379
|
-
return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* Reorder a serialized query string so that params holding the same pairs compare as equal
|
|
383
|
-
* regardless of the order they appear in.
|
|
384
|
-
*
|
|
385
|
-
* @note
|
|
386
|
-
* The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
|
|
387
|
-
* splitting on `&` recovers exactly the pairs and nothing else.
|
|
388
|
-
*/
|
|
389
|
-
function sortSearchParams(serialized) {
|
|
390
|
-
return serialized.split('&').sort().join('&');
|
|
391
|
-
}
|
|
392
|
-
function isPropertyEqual(a, b, state, property) {
|
|
393
|
-
if ((property === REACT_OWNER || property === PREACT_OWNER || property === PREACT_VNODE)
|
|
394
|
-
&& (a.$$typeof || b.$$typeof)) {
|
|
395
|
-
return true;
|
|
396
|
-
}
|
|
397
|
-
return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
|
|
398
|
-
}
|
|
399
493
|
|
|
400
494
|
const { toString } = Object.prototype;
|
|
401
495
|
/**
|
|
@@ -523,14 +617,14 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
523
617
|
areArrayBuffersEqual,
|
|
524
618
|
areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
|
|
525
619
|
areDataViewsEqual,
|
|
526
|
-
areDatesEqual:
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
// `name` / `message` / `stack`
|
|
530
|
-
// comparator of their own rather than being treated as plain objects
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
620
|
+
areDatesEqual: strict
|
|
621
|
+
? combineComparators(areDatesEqual, areObjectsEqualStrict)
|
|
622
|
+
: areDatesEqual,
|
|
623
|
+
// `name` / `message` / `stack` / `cause` / `errors` are not enumerable (unless assigned), which is why
|
|
624
|
+
// errors need a comparator of their own rather than being treated as plain objects. Each variant also
|
|
625
|
+
// compares the own properties that `Error` subclasses routinely carry (`status`, `code`, ...):
|
|
626
|
+
// enumerable ones in loose mode, and all of them (via `areObjectsEqualStrict`) in strict mode.
|
|
627
|
+
areErrorsEqual: strict ? areErrorsEqualStrict : areErrorsEqual,
|
|
534
628
|
areFunctionsEqual: strictEqual,
|
|
535
629
|
areMapsEqual: strict ? combineComparators(areMapsEqual, areObjectsEqualStrict) : areMapsEqual,
|
|
536
630
|
areNumbersEqual: sameValueEqual,
|
|
@@ -538,9 +632,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
538
632
|
arePrimitiveWrappersEqual: arePrimitiveWrappersEqual,
|
|
539
633
|
areRegExpsEqual: areRegExpsEqual,
|
|
540
634
|
areSetsEqual: strict ? combineComparators(areSetsEqual, areObjectsEqualStrict) : areSetsEqual,
|
|
541
|
-
areTypedArraysEqual: strict
|
|
542
|
-
? combineComparators(areTypedArraysEqual, areObjectsEqualStrict)
|
|
543
|
-
: areTypedArraysEqual,
|
|
635
|
+
areTypedArraysEqual: strict ? areTypedArraysEqualStrict : areTypedArraysEqual,
|
|
544
636
|
areUrlsEqual: areUrlsEqual,
|
|
545
637
|
getUnsupportedCustomComparator: undefined,
|
|
546
638
|
strictNullPrototypeComparison,
|
|
@@ -550,6 +642,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
550
642
|
}
|
|
551
643
|
if (circular) {
|
|
552
644
|
const areArraysEqual = createIsCircular(config.areArraysEqual);
|
|
645
|
+
const areDatesEqual = strict ? createIsCircular(config.areDatesEqual) : config.areDatesEqual;
|
|
553
646
|
// Errors are included because comparing `cause` and own properties by value means an error
|
|
554
647
|
// that references itself would otherwise recurse without bound.
|
|
555
648
|
const areErrorsEqual = createIsCircular(config.areErrorsEqual);
|
|
@@ -558,6 +651,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
558
651
|
const areSetsEqual = createIsCircular(config.areSetsEqual);
|
|
559
652
|
config = Object.assign({}, config, {
|
|
560
653
|
areArraysEqual,
|
|
654
|
+
areDatesEqual,
|
|
561
655
|
areErrorsEqual,
|
|
562
656
|
areMapsEqual,
|
|
563
657
|
areObjectsEqual,
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -43,7 +43,8 @@ interface ComparatorConfig<Meta> {
|
|
|
43
43
|
*/
|
|
44
44
|
areDataViewsEqual: EqualityComparator<Meta>;
|
|
45
45
|
/**
|
|
46
|
-
* Whether the dates passed are equal in value.
|
|
46
|
+
* Whether the dates passed are equal in value. In strict mode, this includes
|
|
47
|
+
* additional properties added to the date.
|
|
47
48
|
*/
|
|
48
49
|
areDatesEqual: EqualityComparator<Meta>;
|
|
49
50
|
/**
|
package/dist/es/index.d.mts
CHANGED
|
@@ -43,7 +43,8 @@ interface ComparatorConfig<Meta> {
|
|
|
43
43
|
*/
|
|
44
44
|
areDataViewsEqual: EqualityComparator<Meta>;
|
|
45
45
|
/**
|
|
46
|
-
* Whether the dates passed are equal in value.
|
|
46
|
+
* Whether the dates passed are equal in value. In strict mode, this includes
|
|
47
|
+
* additional properties added to the date.
|
|
47
48
|
*/
|
|
48
49
|
areDatesEqual: EqualityComparator<Meta>;
|
|
49
50
|
/**
|
package/dist/es/index.mjs
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
|
-
const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
|
|
1
|
+
const { getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols } = Object;
|
|
2
2
|
const { hasOwnProperty, toString: toString$1 } = Object.prototype;
|
|
3
3
|
const OBJECT_TAG = '[object Object]';
|
|
4
|
+
/**
|
|
5
|
+
* Whether the own properties from `end` onward are equal with strict property checking, where the
|
|
6
|
+
* caller has already verified that both objects have the same number of own properties.
|
|
7
|
+
*/
|
|
8
|
+
function areStrictPropertiesEqual(a, b, state, properties, end) {
|
|
9
|
+
let index = properties.length;
|
|
10
|
+
let property;
|
|
11
|
+
let descriptorA;
|
|
12
|
+
let descriptorB;
|
|
13
|
+
// Decrementing `while` showed faster results than either incrementing or
|
|
14
|
+
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
15
|
+
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
16
|
+
// cost of anonymous callbacks.
|
|
17
|
+
while (index-- > end) {
|
|
18
|
+
property = properties[index];
|
|
19
|
+
if (!isReactProperty(a, b, property)
|
|
20
|
+
&& (!hasOwn(b, property) || !state.equals(a[property], b[property], property, property, a, b, state))) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
descriptorA = getOwnPropertyDescriptor(a, property);
|
|
24
|
+
descriptorB = getOwnPropertyDescriptor(b, property);
|
|
25
|
+
if ((descriptorA || descriptorB)
|
|
26
|
+
&& (!descriptorA
|
|
27
|
+
|| !descriptorB
|
|
28
|
+
|| descriptorA.configurable !== descriptorB.configurable
|
|
29
|
+
|| descriptorA.enumerable !== descriptorB.enumerable
|
|
30
|
+
|| descriptorA.writable !== descriptorB.writable)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
4
36
|
/**
|
|
5
37
|
* Combine two comparators into a single comparators.
|
|
6
38
|
*/
|
|
@@ -43,6 +75,10 @@ function getStrictProperties(object) {
|
|
|
43
75
|
? getOwnPropertyNames(object).concat(symbols)
|
|
44
76
|
: getOwnPropertyNames(object);
|
|
45
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Identify the element type of TypedArrays across realms and subclasses.
|
|
80
|
+
*/
|
|
81
|
+
const getTypedArrayTag = getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
|
|
46
82
|
/**
|
|
47
83
|
* Whether the object contains the property passed as an own property.
|
|
48
84
|
*/
|
|
@@ -65,15 +101,37 @@ function isNullPrototypeComparable(a, b) {
|
|
|
65
101
|
}
|
|
66
102
|
return false;
|
|
67
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Whether the property passed is a property specific to React/Preact elements. These are ignored in comparison, as they
|
|
106
|
+
* are deeply circular and will return frequent false negatives for the comparison that matters, which is often the
|
|
107
|
+
* React element's aspects (`props`, `type`, etc.).
|
|
108
|
+
*/
|
|
109
|
+
function isReactProperty(a, b, property) {
|
|
110
|
+
return (
|
|
111
|
+
// _owner -> React owner
|
|
112
|
+
// __o -> Preact owner
|
|
113
|
+
// __v -> Preact vnode
|
|
114
|
+
(property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Reorder a serialized query string so that params holding the same pairs compare as equal
|
|
118
|
+
* regardless of the order they appear in.
|
|
119
|
+
*
|
|
120
|
+
* @note
|
|
121
|
+
* The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
|
|
122
|
+
* splitting on `&` recovers exactly the pairs and nothing else.
|
|
123
|
+
*/
|
|
124
|
+
function sortSearchParams(serialized) {
|
|
125
|
+
return serialized.split('&').sort().join('&');
|
|
126
|
+
}
|
|
68
127
|
|
|
69
|
-
const PREACT_VNODE = '__v';
|
|
70
|
-
const PREACT_OWNER = '__o';
|
|
71
|
-
const REACT_OWNER = '_owner';
|
|
72
|
-
const { getOwnPropertyDescriptor, keys } = Object;
|
|
73
128
|
/**
|
|
74
|
-
*
|
|
129
|
+
* Above this many keys, out-of-position keys are checked with `propertyIsEnumerable` rather than by
|
|
130
|
+
* scanning the other object's keys. Measured in V8: scanning wins up to 50 keys and loses from 100.
|
|
75
131
|
*/
|
|
76
|
-
const
|
|
132
|
+
const MAX_SCANNED_PROPERTIES = 64;
|
|
133
|
+
const { keys } = Object;
|
|
134
|
+
const { propertyIsEnumerable } = Object.prototype;
|
|
77
135
|
/**
|
|
78
136
|
* Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
|
|
79
137
|
* Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
|
|
@@ -147,16 +205,68 @@ function areDatesEqual(a, b) {
|
|
|
147
205
|
*
|
|
148
206
|
* @note
|
|
149
207
|
* `name`, `message` and `stack` are own properties but are not enumerable, so they are compared
|
|
150
|
-
* explicitly. `cause`
|
|
151
|
-
* nested value in the comparison is treated.
|
|
152
|
-
*
|
|
153
|
-
*
|
|
208
|
+
* explicitly. `cause` and `errors` are compared by value rather than by reference, matching how
|
|
209
|
+
* every other nested value in the comparison is treated. They are compared exactly once, regardless
|
|
210
|
+
* of whether they were assigned (enumerable) or set by the constructor (non-enumerable). Other own
|
|
211
|
+
* enumerable properties (which custom `Error` subclasses commonly add) are compared afterward.
|
|
212
|
+
*
|
|
213
|
+
* The own property comparison is inlined rather than extracted, which measured faster in V8 when
|
|
214
|
+
* recursing through `cause`. Keep the bytecode under V8's 460-byte TurboFan inlining limit (383 bytes
|
|
215
|
+
* at time of writing; check with `node --print-bytecode --print-bytecode-filter=areErrorsEqual`), or
|
|
216
|
+
* this stops being inlined into its callers.
|
|
154
217
|
*/
|
|
155
218
|
function areErrorsEqual(a, b, state) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
219
|
+
if (a.name !== b.name || a.message !== b.message || a.stack !== b.stack) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
const causeA = a.cause;
|
|
223
|
+
const causeB = b.cause;
|
|
224
|
+
if (causeA !== causeB && !state.equals(causeA, causeB, 'cause', 'cause', a, b, state)) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
const errorsA = a.errors;
|
|
228
|
+
const errorsB = b.errors;
|
|
229
|
+
if (errorsA !== errorsB && !state.equals(errorsA, errorsB, 'errors', 'errors', a, b, state)) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const propertiesA = keys(a);
|
|
233
|
+
const propertiesB = keys(b);
|
|
234
|
+
let index = propertiesA.length;
|
|
235
|
+
// Constructed errors rarely have enumerable own properties.
|
|
236
|
+
if (!index && !propertiesB.length) {
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
let compared = 0;
|
|
240
|
+
let property;
|
|
241
|
+
while (index-- > 0) {
|
|
242
|
+
property = propertiesA[index];
|
|
243
|
+
// Already compared above regardless of enumerability, so we can ignore them.
|
|
244
|
+
if (property === 'cause' || property === 'errors') {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
// `propertyIsEnumerable` rather than `hasOwn`, so that a non-enumerable own property on `b` cannot
|
|
248
|
+
// stand in for an enumerable one. Keys in the same position are trivially enumerable on `b`.
|
|
249
|
+
if ((property !== propertiesB[index] && !propertyIsEnumerable.call(b, property))
|
|
250
|
+
|| !state.equals(a[property], b[property], property, property, a, b, state)) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
++compared;
|
|
254
|
+
}
|
|
255
|
+
// Every compared key is enumerable on `b`, so any of `b`'s keys left over must be `cause` / `errors`.
|
|
256
|
+
const remaining = propertiesB.length - compared;
|
|
257
|
+
return (!remaining
|
|
258
|
+
|| remaining === (propertyIsEnumerable.call(b, 'cause') ? 1 : 0) + (propertyIsEnumerable.call(b, 'errors') ? 1 : 0));
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Whether the errors passed are equal in value.
|
|
262
|
+
*
|
|
263
|
+
* @note
|
|
264
|
+
* `name`, `message` and `stack` are own properties but are not enumerable, so they are compared
|
|
265
|
+
* explicitly. `cause` and `errors` are own properties, so `areObjectsEqualStrict` compares them along
|
|
266
|
+
* with every other own property; they are not compared explicitly to avoid comparing them twice.
|
|
267
|
+
*/
|
|
268
|
+
function areErrorsEqualStrict(a, b, state) {
|
|
269
|
+
return a.name === b.name && a.message === b.message && a.stack === b.stack && areObjectsEqualStrict(a, b, state);
|
|
160
270
|
}
|
|
161
271
|
/**
|
|
162
272
|
* Whether the `Map`s are equal in value.
|
|
@@ -169,7 +279,7 @@ function areMapsEqual(a, b, state) {
|
|
|
169
279
|
if (!size) {
|
|
170
280
|
return true;
|
|
171
281
|
}
|
|
172
|
-
const matchedIndices = new
|
|
282
|
+
const matchedIndices = new Array(size);
|
|
173
283
|
const aIterable = a.entries();
|
|
174
284
|
let aResult;
|
|
175
285
|
let bResult;
|
|
@@ -180,7 +290,7 @@ function areMapsEqual(a, b, state) {
|
|
|
180
290
|
break;
|
|
181
291
|
}
|
|
182
292
|
const bIterable = b.entries();
|
|
183
|
-
let hasMatch =
|
|
293
|
+
let hasMatch = false;
|
|
184
294
|
let matchIndex = 0;
|
|
185
295
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
186
296
|
while ((bResult = bIterable.next())) {
|
|
@@ -195,7 +305,7 @@ function areMapsEqual(a, b, state) {
|
|
|
195
305
|
const bEntry = bResult.value;
|
|
196
306
|
if (state.equals(aEntry[0], bEntry[0], index, matchIndex, a, b, state)
|
|
197
307
|
&& state.equals(aEntry[1], bEntry[1], aEntry[0], bEntry[0], a, b, state)) {
|
|
198
|
-
hasMatch = matchedIndices[matchIndex] =
|
|
308
|
+
hasMatch = matchedIndices[matchIndex] = true;
|
|
199
309
|
break;
|
|
200
310
|
}
|
|
201
311
|
matchIndex++;
|
|
@@ -212,16 +322,30 @@ function areMapsEqual(a, b, state) {
|
|
|
212
322
|
*/
|
|
213
323
|
function areObjectsEqual(a, b, state) {
|
|
214
324
|
const properties = keys(a);
|
|
325
|
+
const propertiesB = keys(b);
|
|
215
326
|
let index = properties.length;
|
|
216
|
-
if (
|
|
327
|
+
if (propertiesB.length !== index) {
|
|
217
328
|
return false;
|
|
218
329
|
}
|
|
330
|
+
const isScannable = index <= MAX_SCANNED_PROPERTIES;
|
|
331
|
+
let property;
|
|
219
332
|
// Decrementing `while` showed faster results than either incrementing or
|
|
220
333
|
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
221
334
|
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
222
335
|
// cost of anonymous callbacks.
|
|
223
336
|
while (index-- > 0) {
|
|
224
|
-
|
|
337
|
+
property = properties[index];
|
|
338
|
+
if (isReactProperty(a, b, property)) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
// Objects built the same way list their keys in the same order, in which case the key is
|
|
342
|
+
// already known to be an own enumerable property of `b` and the lookup can be skipped.
|
|
343
|
+
// Otherwise, `propertiesB` is exactly `b`'s own enumerable keys. Scanning it is faster than
|
|
344
|
+
// `propertyIsEnumerable` for smaller objects, but it is linear per key, so larger objects
|
|
345
|
+
// (where reordering would make it quadratic) use the lookup instead.
|
|
346
|
+
if ((property !== propertiesB[index]
|
|
347
|
+
&& !(isScannable ? propertiesB.includes(property) : propertyIsEnumerable.call(b, property)))
|
|
348
|
+
|| !state.equals(a[property], b[property], property, property, a, b, state)) {
|
|
225
349
|
return false;
|
|
226
350
|
}
|
|
227
351
|
}
|
|
@@ -232,34 +356,7 @@ function areObjectsEqual(a, b, state) {
|
|
|
232
356
|
*/
|
|
233
357
|
function areObjectsEqualStrict(a, b, state) {
|
|
234
358
|
const properties = getStrictProperties(a);
|
|
235
|
-
|
|
236
|
-
if (getStrictProperties(b).length !== index) {
|
|
237
|
-
return false;
|
|
238
|
-
}
|
|
239
|
-
let property;
|
|
240
|
-
let descriptorA;
|
|
241
|
-
let descriptorB;
|
|
242
|
-
// Decrementing `while` showed faster results than either incrementing or
|
|
243
|
-
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
244
|
-
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
245
|
-
// cost of anonymous callbacks.
|
|
246
|
-
while (index-- > 0) {
|
|
247
|
-
property = properties[index];
|
|
248
|
-
if (!isPropertyEqual(a, b, state, property)) {
|
|
249
|
-
return false;
|
|
250
|
-
}
|
|
251
|
-
descriptorA = getOwnPropertyDescriptor(a, property);
|
|
252
|
-
descriptorB = getOwnPropertyDescriptor(b, property);
|
|
253
|
-
if ((descriptorA || descriptorB)
|
|
254
|
-
&& (!descriptorA
|
|
255
|
-
|| !descriptorB
|
|
256
|
-
|| descriptorA.configurable !== descriptorB.configurable
|
|
257
|
-
|| descriptorA.enumerable !== descriptorB.enumerable
|
|
258
|
-
|| descriptorA.writable !== descriptorB.writable)) {
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return true;
|
|
359
|
+
return getStrictProperties(b).length === properties.length && areStrictPropertiesEqual(a, b, state, properties, 0);
|
|
263
360
|
}
|
|
264
361
|
/**
|
|
265
362
|
* Whether the primitive wrappers passed are equal in value.
|
|
@@ -273,6 +370,21 @@ function arePrimitiveWrappersEqual(a, b) {
|
|
|
273
370
|
function areRegExpsEqual(a, b) {
|
|
274
371
|
return a.source === b.source && a.flags === b.flags;
|
|
275
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Whether the search params passed are equal in value.
|
|
375
|
+
*
|
|
376
|
+
* @note
|
|
377
|
+
* Order is not significant, matching how the other unordered collections in the library are
|
|
378
|
+
* compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
|
|
379
|
+
* `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
|
|
380
|
+
*/
|
|
381
|
+
function areSearchParamsEqual(a, b) {
|
|
382
|
+
const serializedA = a.toString();
|
|
383
|
+
const serializedB = b.toString();
|
|
384
|
+
// Identical serializations are equal under any ordering, and this is by far the common case, so
|
|
385
|
+
// it is worth checking before sorting anything.
|
|
386
|
+
return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
|
|
387
|
+
}
|
|
276
388
|
/**
|
|
277
389
|
* Whether the `Set`s are equal in value.
|
|
278
390
|
*/
|
|
@@ -284,7 +396,7 @@ function areSetsEqual(a, b, state) {
|
|
|
284
396
|
if (!size) {
|
|
285
397
|
return true;
|
|
286
398
|
}
|
|
287
|
-
const matchedIndices = new
|
|
399
|
+
const matchedIndices = new Array(size);
|
|
288
400
|
const aIterable = a.values();
|
|
289
401
|
let aResult;
|
|
290
402
|
let bResult;
|
|
@@ -294,7 +406,7 @@ function areSetsEqual(a, b, state) {
|
|
|
294
406
|
break;
|
|
295
407
|
}
|
|
296
408
|
const bIterable = b.values();
|
|
297
|
-
let hasMatch =
|
|
409
|
+
let hasMatch = false;
|
|
298
410
|
let matchIndex = 0;
|
|
299
411
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
300
412
|
while ((bResult = bIterable.next())) {
|
|
@@ -303,7 +415,7 @@ function areSetsEqual(a, b, state) {
|
|
|
303
415
|
}
|
|
304
416
|
if (!matchedIndices[matchIndex]
|
|
305
417
|
&& state.equals(aResult.value, bResult.value, aResult.value, bResult.value, a, b, state)) {
|
|
306
|
-
hasMatch = matchedIndices[matchIndex] =
|
|
418
|
+
hasMatch = matchedIndices[matchIndex] = true;
|
|
307
419
|
break;
|
|
308
420
|
}
|
|
309
421
|
matchIndex++;
|
|
@@ -343,6 +455,21 @@ function areTypedArraysEqual(a, b) {
|
|
|
343
455
|
}
|
|
344
456
|
return true;
|
|
345
457
|
}
|
|
458
|
+
/**
|
|
459
|
+
* Whether the TypedArray instances are equal in value with strict property checking.
|
|
460
|
+
*
|
|
461
|
+
* @note
|
|
462
|
+
* Indexed elements are also own properties, listed first and in order, with descriptors fixed by the
|
|
463
|
+
* spec. They are compared once by `areTypedArraysEqual`, so only the own properties after them
|
|
464
|
+
* (including symbols) are compared with strict property checking.
|
|
465
|
+
*/
|
|
466
|
+
function areTypedArraysEqualStrict(a, b, state) {
|
|
467
|
+
if (!areTypedArraysEqual(a, b)) {
|
|
468
|
+
return false;
|
|
469
|
+
}
|
|
470
|
+
const properties = getStrictProperties(a);
|
|
471
|
+
return (getStrictProperties(b).length === properties.length && areStrictPropertiesEqual(a, b, state, properties, a.length));
|
|
472
|
+
}
|
|
346
473
|
/**
|
|
347
474
|
* Whether the URL instances are equal in value.
|
|
348
475
|
*/
|
|
@@ -361,39 +488,6 @@ function areUrlsEqual(a, b) {
|
|
|
361
488
|
&& a.hash === b.hash
|
|
362
489
|
&& areSearchParamsEqual(a.searchParams, b.searchParams));
|
|
363
490
|
}
|
|
364
|
-
/**
|
|
365
|
-
* Whether the search params passed are equal in value.
|
|
366
|
-
*
|
|
367
|
-
* @note
|
|
368
|
-
* Order is not significant, matching how the other unordered collections in the library are
|
|
369
|
-
* compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
|
|
370
|
-
* `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
|
|
371
|
-
*/
|
|
372
|
-
function areSearchParamsEqual(a, b) {
|
|
373
|
-
const serializedA = a.toString();
|
|
374
|
-
const serializedB = b.toString();
|
|
375
|
-
// Identical serializations are equal under any ordering, and this is by far the common case, so
|
|
376
|
-
// it is worth checking before sorting anything.
|
|
377
|
-
return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* Reorder a serialized query string so that params holding the same pairs compare as equal
|
|
381
|
-
* regardless of the order they appear in.
|
|
382
|
-
*
|
|
383
|
-
* @note
|
|
384
|
-
* The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
|
|
385
|
-
* splitting on `&` recovers exactly the pairs and nothing else.
|
|
386
|
-
*/
|
|
387
|
-
function sortSearchParams(serialized) {
|
|
388
|
-
return serialized.split('&').sort().join('&');
|
|
389
|
-
}
|
|
390
|
-
function isPropertyEqual(a, b, state, property) {
|
|
391
|
-
if ((property === REACT_OWNER || property === PREACT_OWNER || property === PREACT_VNODE)
|
|
392
|
-
&& (a.$$typeof || b.$$typeof)) {
|
|
393
|
-
return true;
|
|
394
|
-
}
|
|
395
|
-
return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
|
|
396
|
-
}
|
|
397
491
|
|
|
398
492
|
const { toString } = Object.prototype;
|
|
399
493
|
/**
|
|
@@ -521,14 +615,14 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
521
615
|
areArrayBuffersEqual,
|
|
522
616
|
areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
|
|
523
617
|
areDataViewsEqual,
|
|
524
|
-
areDatesEqual:
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
// `name` / `message` / `stack`
|
|
528
|
-
// comparator of their own rather than being treated as plain objects
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
618
|
+
areDatesEqual: strict
|
|
619
|
+
? combineComparators(areDatesEqual, areObjectsEqualStrict)
|
|
620
|
+
: areDatesEqual,
|
|
621
|
+
// `name` / `message` / `stack` / `cause` / `errors` are not enumerable (unless assigned), which is why
|
|
622
|
+
// errors need a comparator of their own rather than being treated as plain objects. Each variant also
|
|
623
|
+
// compares the own properties that `Error` subclasses routinely carry (`status`, `code`, ...):
|
|
624
|
+
// enumerable ones in loose mode, and all of them (via `areObjectsEqualStrict`) in strict mode.
|
|
625
|
+
areErrorsEqual: strict ? areErrorsEqualStrict : areErrorsEqual,
|
|
532
626
|
areFunctionsEqual: strictEqual,
|
|
533
627
|
areMapsEqual: strict ? combineComparators(areMapsEqual, areObjectsEqualStrict) : areMapsEqual,
|
|
534
628
|
areNumbersEqual: sameValueEqual,
|
|
@@ -536,9 +630,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
536
630
|
arePrimitiveWrappersEqual: arePrimitiveWrappersEqual,
|
|
537
631
|
areRegExpsEqual: areRegExpsEqual,
|
|
538
632
|
areSetsEqual: strict ? combineComparators(areSetsEqual, areObjectsEqualStrict) : areSetsEqual,
|
|
539
|
-
areTypedArraysEqual: strict
|
|
540
|
-
? combineComparators(areTypedArraysEqual, areObjectsEqualStrict)
|
|
541
|
-
: areTypedArraysEqual,
|
|
633
|
+
areTypedArraysEqual: strict ? areTypedArraysEqualStrict : areTypedArraysEqual,
|
|
542
634
|
areUrlsEqual: areUrlsEqual,
|
|
543
635
|
getUnsupportedCustomComparator: undefined,
|
|
544
636
|
strictNullPrototypeComparison,
|
|
@@ -548,6 +640,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
548
640
|
}
|
|
549
641
|
if (circular) {
|
|
550
642
|
const areArraysEqual = createIsCircular(config.areArraysEqual);
|
|
643
|
+
const areDatesEqual = strict ? createIsCircular(config.areDatesEqual) : config.areDatesEqual;
|
|
551
644
|
// Errors are included because comparing `cause` and own properties by value means an error
|
|
552
645
|
// that references itself would otherwise recurse without bound.
|
|
553
646
|
const areErrorsEqual = createIsCircular(config.areErrorsEqual);
|
|
@@ -556,6 +649,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
|
|
|
556
649
|
const areSetsEqual = createIsCircular(config.areSetsEqual);
|
|
557
650
|
config = Object.assign({}, config, {
|
|
558
651
|
areArraysEqual,
|
|
652
|
+
areDatesEqual,
|
|
559
653
|
areErrorsEqual,
|
|
560
654
|
areMapsEqual,
|
|
561
655
|
areObjectsEqual,
|
package/index.d.ts
CHANGED
|
@@ -43,7 +43,8 @@ interface ComparatorConfig<Meta> {
|
|
|
43
43
|
*/
|
|
44
44
|
areDataViewsEqual: EqualityComparator<Meta>;
|
|
45
45
|
/**
|
|
46
|
-
* Whether the dates passed are equal in value.
|
|
46
|
+
* Whether the dates passed are equal in value. In strict mode, this includes
|
|
47
|
+
* additional properties added to the date.
|
|
47
48
|
*/
|
|
48
49
|
areDatesEqual: EqualityComparator<Meta>;
|
|
49
50
|
/**
|
package/package.json
CHANGED
|
@@ -11,12 +11,12 @@
|
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@planttheidea/build-tools": "^2.0.8",
|
|
13
13
|
"@types/lodash": "^4.17.25",
|
|
14
|
-
"@types/node": "^24.
|
|
14
|
+
"@types/node": "^24.19.1",
|
|
15
15
|
"@types/ramda": "^0.32.0",
|
|
16
|
-
"@types/react": "^19.
|
|
17
|
-
"@types/react-dom": "^19.
|
|
18
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
19
|
-
"@typescript-eslint/parser": "^8.
|
|
16
|
+
"@types/react": "^19.3.0",
|
|
17
|
+
"@types/react-dom": "^19.3.0",
|
|
18
|
+
"@typescript-eslint/eslint-plugin": "^8.71.1",
|
|
19
|
+
"@typescript-eslint/parser": "^8.71.1",
|
|
20
20
|
"@vitest/coverage-v8": "^4.1.11",
|
|
21
21
|
"cli-table3": "^0.6.5",
|
|
22
22
|
"decircularize": "^1.0.0",
|
|
@@ -31,18 +31,18 @@
|
|
|
31
31
|
"fast-glob": "^3.3.3",
|
|
32
32
|
"lodash": "^4.18.1",
|
|
33
33
|
"nano-equal": "^2.0.2",
|
|
34
|
-
"prettier": "^3.9.
|
|
35
|
-
"react": "^19.
|
|
36
|
-
"react-dom": "^19.
|
|
34
|
+
"prettier": "^3.9.9",
|
|
35
|
+
"react": "^19.3.0",
|
|
36
|
+
"react-dom": "^19.3.0",
|
|
37
37
|
"react-fast-compare": "^3.2.2",
|
|
38
|
-
"release-it": "^21.
|
|
39
|
-
"rollup": "^4.
|
|
38
|
+
"release-it": "^21.1.1",
|
|
39
|
+
"rollup": "^4.64.3",
|
|
40
40
|
"shallow-equal-fuzzy": "^0.0.2",
|
|
41
|
-
"tinybench": "^6.1
|
|
41
|
+
"tinybench": "^6.2.1",
|
|
42
42
|
"typescript": "^6.0.3",
|
|
43
|
-
"typescript-eslint": "^8.
|
|
43
|
+
"typescript-eslint": "^8.71.1",
|
|
44
44
|
"underscore": "^1.13.8",
|
|
45
|
-
"vite": "^8.
|
|
45
|
+
"vite": "^8.3.4",
|
|
46
46
|
"vitest": "^4.1.11"
|
|
47
47
|
},
|
|
48
48
|
"engines": {
|
|
@@ -110,5 +110,5 @@
|
|
|
110
110
|
"sideEffects": false,
|
|
111
111
|
"type": "module",
|
|
112
112
|
"types": "./index.d.ts",
|
|
113
|
-
"version": "6.
|
|
113
|
+
"version": "6.2.1"
|
|
114
114
|
}
|