fast-equals 6.0.4 → 6.1.0

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 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.3kB when minified and gzipped.
4
+ various use-cases. It has no dependencies, and is ~2.46kB when minified and gzipped.
5
5
 
6
6
  The following types are handled out-of-the-box:
7
7
 
@@ -17,8 +17,8 @@ The following types are handled out-of-the-box:
17
17
  - Primitive wrappers (`new Boolean()` / `new Number()` / `new String()`, and boxed `BigInt`)
18
18
  - Custom class instances, including subclasses of native classes
19
19
 
20
- Methods are available for deep, shallow, [`SameValue`](http://ecma-international.org/ecma-262/7.0/#sec-samevalue),
21
- [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero), or
20
+ Methods are available for deep, shallow, [`SameValue`](https://262.ecma-international.org/7.0/#sec-samevalue),
21
+ [`SameValueZero`](https://262.ecma-international.org/7.0/#sec-samevaluezero), or
22
22
  [strict equality](https://262.ecma-international.org/7.0/#sec-strict-equality-comparison) comparison. In addition, you
23
23
  can opt into support for circular objects, or performing a "strict" comparison with unconventional property definition,
24
24
  or both. You can also customize any specific type comparison based on your application's use-cases.
@@ -47,6 +47,7 @@ If you are having any problems, want to request a new feature, or have any quest
47
47
  - [strictCircularDeepEqual](#strictcirculardeepequal)
48
48
  - [strictCircularShallowEqual](#strictcircularshallowequal)
49
49
  - [createCustomEqual](#createcustomequal)
50
+ - [strictNullPrototypeComparison](#strictnullprototypecomparison)
50
51
  - [getUnsupportedCustomComparator](#getunsupportedcustomcomparator)
51
52
  - [Recipes](#recipes)
52
53
  - [Benchmarks](#benchmarks)
@@ -79,7 +80,7 @@ console.log(deepEqual(objectA, objectB)); // true
79
80
  #### Comparing `Map`s
80
81
 
81
82
  `Map` objects support complex keys (objects, Arrays, etc.), however
82
- [the spec for key lookups in `Map` are based on `SameZeroValue`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#key_equality).
83
+ [the spec for key lookups in `Map` are based on `SameValueZero`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#key_equality).
83
84
  If the spec were followed for comparison, the following would always be `false`:
84
85
 
85
86
  ```ts
@@ -113,8 +114,8 @@ console.log(shallowEqual(objectA, objectC)); // false
113
114
 
114
115
  ### sameValueEqual
115
116
 
116
- Performs a [`SameValue`](http://ecma-international.org/ecma-262/7.0/#sec-samevalue) comparison on the two objects passed
117
- and returns a boolean representing the value equivalency of the objects. In simple terms, this means:
117
+ Performs a [`SameValue`](https://262.ecma-international.org/7.0/#sec-samevalue) comparison on the two objects passed and
118
+ returns a boolean representing the value equivalency of the objects. In simple terms, this means:
118
119
 
119
120
  - `+0` and `-0` are not equal
120
121
  - `NaN` is equal to `NaN`
@@ -140,15 +141,15 @@ _**NOTE**: In environments that support
140
141
 
141
142
  ### sameValueZeroEqual
142
143
 
143
- Performs a [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) comparison on the two
144
- objects passed and returns a boolean representing the value equivalency of the objects. In simple terms, this means:
144
+ Performs a [`SameValueZero`](https://262.ecma-international.org/7.0/#sec-samevaluezero) comparison on the two objects
145
+ passed and returns a boolean representing the value equivalency of the objects. In simple terms, this means:
145
146
 
146
147
  - `+0` and `-0` are equal
147
148
  - `NaN` is equal to `NaN`
148
149
  - All other items are based on referential equality (`a === b`)
149
150
 
150
151
  ```ts
151
- import { sameValueEqual } from 'fast-equals';
152
+ import { sameValueZeroEqual } from 'fast-equals';
152
153
 
153
154
  const mainObject = { foo: NaN, bar: 'baz' };
154
155
 
@@ -156,9 +157,9 @@ const objectA = 'baz';
156
157
  const objectB = NaN;
157
158
  const objectC = { foo: NaN, bar: 'baz' };
158
159
 
159
- console.log(sameValueEqual(mainObject.bar, objectA)); // true
160
- console.log(sameValueEqual(mainObject.foo, objectB)); // true
161
- console.log(sameValueEqual(mainObject, objectC)); // false
160
+ console.log(sameValueZeroEqual(mainObject.bar, objectA)); // true
161
+ console.log(sameValueZeroEqual(mainObject.foo, objectB)); // true
162
+ console.log(sameValueZeroEqual(mainObject, objectC)); // false
162
163
  ```
163
164
 
164
165
  ### strictEqual
@@ -180,9 +181,9 @@ const objectA = 'baz';
180
181
  const objectB = NaN;
181
182
  const objectC = { foo: NaN, bar: 'baz' };
182
183
 
183
- console.log(sameValueEqual(mainObject.bar, objectA)); // true
184
- console.log(sameValueEqual(mainObject.foo, objectB)); // false
185
- console.log(sameValueEqual(mainObject, objectC)); // false
184
+ console.log(strictEqual(mainObject.bar, objectA)); // true
185
+ console.log(strictEqual(mainObject.foo, objectB)); // false
186
+ console.log(strictEqual(mainObject, objectC)); // false
186
187
  ```
187
188
 
188
189
  _**NOTE**: This is mainly a convenience function, such as needing a default functional equality comparator. Naturally,
@@ -213,7 +214,7 @@ Just as with `deepEqual`, [both keys and values are compared for deep equality](
213
214
 
214
215
  ### circularShallowEqual
215
216
 
216
- Performs the same comparison as `shallowequal` but supports circular objects. It is slower than `shallowEqual`, so only
217
+ Performs the same comparison as `shallowEqual` but supports circular objects. It is slower than `shallowEqual`, so only
217
218
  use if you know circular objects are present.
218
219
 
219
220
  ```ts
@@ -227,7 +228,7 @@ console.log(circularShallowEqual(array, [array])); // false
227
228
 
228
229
  ### strictDeepEqual
229
230
 
230
- Performs the same comparison as `deepEqual` but performs a strict comparison of the objects. In this includes:
231
+ Performs the same comparison as `deepEqual` but performs a strict comparison of the objects. This includes:
231
232
 
232
233
  - Checking symbol properties
233
234
  - Checking non-enumerable properties in object comparisons
@@ -242,13 +243,13 @@ const otherArray = [{ foo: 'bar' }];
242
243
  array.bar = 'baz';
243
244
  otherArray.bar = 'baz';
244
245
 
245
- console.log(strictDeepEqual(array, otherArray)); // true;
246
- console.log(strictDeepEqual(array, [{ foo: 'bar' }])); // false;
246
+ console.log(strictDeepEqual(array, otherArray)); // true
247
+ console.log(strictDeepEqual(array, [{ foo: 'bar' }])); // false
247
248
  ```
248
249
 
249
250
  ### strictShallowEqual
250
251
 
251
- Performs the same comparison as `shallowEqual` but performs a strict comparison of the objects. In this includes:
252
+ Performs the same comparison as `shallowEqual` but performs a strict comparison of the objects. This includes:
252
253
 
253
254
  - Checking non-enumerable properties in object comparisons
254
255
  - Checking full descriptor of properties on the object to match
@@ -262,13 +263,13 @@ const otherArray = ['foo'];
262
263
  array.bar = 'baz';
263
264
  otherArray.bar = 'baz';
264
265
 
265
- console.log(strictDeepEqual(array, otherArray)); // true;
266
- console.log(strictDeepEqual(array, ['foo'])); // false;
266
+ console.log(strictShallowEqual(array, otherArray)); // true
267
+ console.log(strictShallowEqual(array, ['foo'])); // false
267
268
  ```
268
269
 
269
270
  ### strictCircularDeepEqual
270
271
 
271
- Performs the same comparison as `circularDeepEqual` but performs a strict comparison of the objects. In this includes:
272
+ Performs the same comparison as `circularDeepEqual` but performs a strict comparison of the objects. This includes:
272
273
 
273
274
  - Checking `Symbol` properties on the object
274
275
  - Checking non-enumerable properties in object comparisons
@@ -302,14 +303,13 @@ Object.defineProperty(second, 'bar', {
302
303
  value: 'baz',
303
304
  });
304
305
 
305
- console.log(circularDeepEqual(first, second)); // true
306
- console.log(circularDeepEqual(first, new Circular('foo'))); // false
306
+ console.log(strictCircularDeepEqual(first, second)); // true
307
+ console.log(strictCircularDeepEqual(first, new Circular('foo'))); // false
307
308
  ```
308
309
 
309
310
  ### strictCircularShallowEqual
310
311
 
311
- Performs the same comparison as `circularShallowEqual` but performs a strict comparison of the objects. In this
312
- includes:
312
+ Performs the same comparison as `circularShallowEqual` but performs a strict comparison of the objects. This includes:
313
313
 
314
314
  - Checking non-enumerable properties in object comparisons
315
315
  - Checking full descriptor of properties on the object to match
@@ -321,13 +321,13 @@ const array = ['foo'];
321
321
  const otherArray = ['foo'];
322
322
 
323
323
  array.push(array);
324
- otherArray.push(otherArray);
324
+ otherArray.push(array); // Shallow comparison requires the same nested reference.
325
325
 
326
326
  array.bar = 'baz';
327
327
  otherArray.bar = 'baz';
328
328
 
329
- console.log(circularShallowEqual(array, otherArray)); // true
330
- console.log(circularShallowEqual(array, ['foo', array])); // false
329
+ console.log(strictCircularShallowEqual(array, otherArray)); // true
330
+ console.log(strictCircularShallowEqual(array, ['foo', array])); // false
331
331
  ```
332
332
 
333
333
  ### createCustomEqual
@@ -360,7 +360,9 @@ interface ComparatorConfig<Meta> {
360
360
  areSetsEqual: EqualityComparator<Meta>;
361
361
  areTypedArraysEqual: EqualityComparator<Meta>;
362
362
  areUrlsEqual: EqualityComparator<Meta>;
363
- getUnsupportedCustomComparator: <Type>(a: Type, b: Type, state: State<Meta>, tag: string) => EqualityComparator<Meta>;
363
+ getUnsupportedCustomComparator:
364
+ (<Type>(a: Type, b: Type, state: State<Meta>, tag: string) => EqualityComparator<Meta> | undefined) | undefined;
365
+ strictNullPrototypeComparison?: boolean;
364
366
  }
365
367
 
366
368
  function createCustomEqual<Meta>(options: {
@@ -371,6 +373,7 @@ function createCustomEqual<Meta>(options: {
371
373
  ) => (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
372
374
  createState?: () => { cache?: Cache; meta?: Meta };
373
375
  strict?: boolean;
376
+ strictNullPrototypeComparison?: boolean;
374
377
  }): <A, B>(a: A, b: B) => boolean;
375
378
  ```
376
379
 
@@ -383,11 +386,29 @@ _**NOTE**: `Map` implementations compare equality for both keys and value. When
383
386
  equality of the keys, the iteration index is provided as both `indexOrKeyA` and `indexOrKeyB` to help use-cases where
384
387
  ordering of keys matters to equality._
385
388
 
389
+ #### strictNullPrototypeComparison
390
+
391
+ Defaults to `true`, which keeps a null-prototype object (`Object.create(null)`) unequal to an otherwise identical plain
392
+ object. Set it to `false` to compare the two as dictionaries, for values such as
393
+ [ProseMirror node attributes](https://prosemirror.net/docs/ref/#model.Node.attrs) or `Object.groupBy` results.
394
+
395
+ ```ts
396
+ const equal = createCustomEqual({ strictNullPrototypeComparison: false });
397
+ const attrs = Object.assign(Object.create(null), { aspectRatio: 16 / 9 });
398
+
399
+ equal(attrs, { ...attrs }); // true
400
+ ```
401
+
402
+ This is the only exception to the constructor check: a null-prototype object remains unequal to arrays, `Map`s, dates,
403
+ and other built-ins, and distinct class constructors remain unequal to each other. It applies to nested values and can
404
+ be combined with `circular`, `strict`, and custom comparators.
405
+
386
406
  #### getUnsupportedCustomComparator
387
407
 
388
- If you want to compare objects that have a custom `@@toStringTag`, you can provide a map of the custom tags you want to
389
- support via the `getUnsupportedCustomComparator` option. See [this recipe]('./recipes/special-objects.md) for an
390
- example.
408
+ If you want to compare objects whose tag is not handled out-of-the-box (such as a custom `Symbol.toStringTag` or a
409
+ `WeakMap`), you can provide a `getUnsupportedCustomComparator` method. It receives both values, the comparison state,
410
+ and the tag, and returns the comparator to use for them. Returning `undefined` treats the values as unequal. See
411
+ [this recipe](./recipes/special-objects.md) for an example.
391
412
 
392
413
  #### Recipes
393
414
 
@@ -396,16 +417,15 @@ to the problem you are solving, they can offer guidance of how to structure your
396
417
 
397
418
  - [Legacy environment support for `RegExp` comparators](./recipes/legacy-regexp-support.md)
398
419
  - [Explicit property check](./recipes/explicit-property-check.md)
399
- - [Using `meta` in comparison](./recipes//using-meta-in-comparison.md)
420
+ - [Using `meta` in comparison](./recipes/using-meta-in-comparison.md)
400
421
  - [Comparing non-standard properties](./recipes/non-standard-properties.md)
401
- - [Strict property descriptor comparison](./recipes/strict-property-descriptor-check.md)
402
422
  - [Legacy environment support for circular equal comparators](./recipes/legacy-circular-equal-support.md)
403
423
  - [Custom comparator support](./recipes/special-objects.md)
404
424
 
405
425
  ## Benchmarks
406
426
 
407
427
  All benchmarks were performed on an i9-11900H Ubuntu Linux 24.04 laptop with 64GB of memory using NodeJS version
408
- `24.11.1`, and are based on averages of running comparisons based deep equality on the following object types:
428
+ `24.15.0`, and are based on averages of running comparisons based deep equality on the following object types:
409
429
 
410
430
  - Primitives (`String`, `Number`, `null`, `undefined`)
411
431
  - `Function`
@@ -421,74 +441,74 @@ Testing mixed objects equal...
421
441
  ┌────────────────────────────────────────┬────────────────┐
422
442
  │ Name │ Ops / sec │
423
443
  ├────────────────────────────────────────┼────────────────┤
424
- │ fast-equals (passed) │ 1478330.935014 │
444
+ │ fast-equals (passed) │ 1522774.1567 │
425
445
  ├────────────────────────────────────────┼────────────────┤
426
- │ fast-deep-equal (passed) │ 1246644.004889 │
446
+ │ react-fast-compare (passed) │ 1285289.880042 │
427
447
  ├────────────────────────────────────────┼────────────────┤
428
- │ react-fast-compare (passed) │ 1223991.602206 │
448
+ │ fast-deep-equal (passed) │ 1285105.414715 │
429
449
  ├────────────────────────────────────────┼────────────────┤
430
- │ shallow-equal-fuzzy (passed) │ 1192746.28367 │
450
+ │ shallow-equal-fuzzy (passed) │ 1248325.039481 │
431
451
  ├────────────────────────────────────────┼────────────────┤
432
- │ nano-equal (failed) │ 917825.058845 │
452
+ │ nano-equal (failed) │ 962152.688616 │
433
453
  ├────────────────────────────────────────┼────────────────┤
434
- │ fast-equals (circular) (passed) │ 734269.031473 │
454
+ │ fast-equals (circular) (passed) │ 800100.693495 │
435
455
  ├────────────────────────────────────────┼────────────────┤
436
- │ dequal/lite (passed) │ 702632.334945 │
456
+ │ dequal/lite (passed) │ 778011.650869 │
437
457
  ├────────────────────────────────────────┼────────────────┤
438
- │ dequal (passed) │ 676206.685725 │
458
+ │ dequal (passed) │ 770187.208947 │
439
459
  ├────────────────────────────────────────┼────────────────┤
440
- │ underscore.isEqual (passed) │ 413859.810046 │
460
+ │ underscore.isEqual (passed) │ 449433.41171 │
441
461
  ├────────────────────────────────────────┼────────────────┤
442
- │ assert.deepStrictEqual (passed) │ 384573.330742 │
462
+ │ assert.deepStrictEqual (passed) │ 442249.829162 │
443
463
  ├────────────────────────────────────────┼────────────────┤
444
- │ es-toolkit (passed) │ 333634.454044 │
464
+ │ es-toolkit (passed) │ 355943.82757 │
445
465
  ├────────────────────────────────────────┼────────────────┤
446
- │ fast-equals (strict) (passed) │ 285124.652706 │
466
+ │ fast-equals (strict) (passed) │ 307931.96677 │
447
467
  ├────────────────────────────────────────┼────────────────┤
448
- │ lodash.isEqual (passed) │ 272221.340481 │
468
+ │ lodash.isEqual (passed) │ 294660.269815 │
449
469
  ├────────────────────────────────────────┼────────────────┤
450
- │ fast-equals (strict circular) (passed) │ 236878.960045 │
470
+ │ fast-equals (strict circular) (passed) │ 252760.794458 │
451
471
  ├────────────────────────────────────────┼────────────────┤
452
- │ deep-eql (passed) │ 154390.610864 │
472
+ │ deep-eql (passed) │ 166131.383227 │
453
473
  ├────────────────────────────────────────┼────────────────┤
454
- │ deep-equal (passed) │ 905.343563 │
474
+ │ deep-equal (passed) │ 970.944208 │
455
475
  └────────────────────────────────────────┴────────────────┘
456
476
 
457
477
  Testing mixed objects not equal...
458
478
  ┌────────────────────────────────────────┬────────────────┐
459
479
  │ Name │ Ops / sec │
460
480
  ├────────────────────────────────────────┼────────────────┤
461
- │ fast-equals (passed) │ 4962887.909706 │
481
+ │ fast-equals (passed) │ 5122472.769802 │
462
482
  ├────────────────────────────────────────┼────────────────┤
463
- │ fast-equals (circular) (passed) │ 3362453.019363 │
483
+ │ fast-deep-equal (passed) │ 3520297.242957 │
464
484
  ├────────────────────────────────────────┼────────────────┤
465
- │ fast-deep-equal (passed) │ 3359178.728738 │
485
+ │ fast-equals (circular) (passed) │ 3485912.9348 │
466
486
  ├────────────────────────────────────────┼────────────────┤
467
- │ react-fast-compare (passed) │ 3240226.145634 │
487
+ │ react-fast-compare (passed) │ 3414203.488651 │
468
488
  ├────────────────────────────────────────┼────────────────┤
469
- │ fast-equals (strict) (passed) │ 2895230.420069 │
489
+ │ fast-equals (strict) (passed) │ 3038182.032793 │
470
490
  ├────────────────────────────────────────┼────────────────┤
471
- │ fast-equals (strict circular) (passed) │ 2178891.848559 │
491
+ │ fast-equals (strict circular) (passed) │ 2344879.08551 │
472
492
  ├────────────────────────────────────────┼────────────────┤
473
- │ dequal/lite (passed) │ 1271343.172786 │
493
+ │ dequal/lite (passed) │ 1340883.333611 │
474
494
  ├────────────────────────────────────────┼────────────────┤
475
- │ dequal (passed) │ 1249090.305916 │
495
+ │ dequal (passed) │ 1311832.728093 │
476
496
  ├────────────────────────────────────────┼────────────────┤
477
- │ shallow-equal-fuzzy (failed) │ 1197792.018762 │
497
+ │ shallow-equal-fuzzy (failed) │ 1249967.301358 │
478
498
  ├────────────────────────────────────────┼────────────────┤
479
- │ nano-equal (passed) │ 1041772.816799 │
499
+ │ nano-equal (passed) │ 1096529.56672 │
480
500
  ├────────────────────────────────────────┼────────────────┤
481
- │ underscore.isEqual (passed) │ 642874.802948 │
501
+ │ underscore.isEqual (passed) │ 660524.165237 │
482
502
  ├────────────────────────────────────────┼────────────────┤
483
- │ lodash.isEqual (passed) │ 360803.14264 │
503
+ │ lodash.isEqual (passed) │ 385500.174796 │
484
504
  ├────────────────────────────────────────┼────────────────┤
485
- │ es-toolkit (passed) │ 353678.240503 │
505
+ │ es-toolkit (passed) │ 365617.977684 │
486
506
  ├────────────────────────────────────────┼────────────────┤
487
- │ deep-eql (passed) │ 177224.890296 │
507
+ │ deep-eql (passed) │ 186388.440331 │
488
508
  ├────────────────────────────────────────┼────────────────┤
489
- │ assert.deepStrictEqual (passed) │ 19507.749026 │
509
+ │ assert.deepStrictEqual (passed) │ 21060.162935 │
490
510
  ├────────────────────────────────────────┼────────────────┤
491
- │ deep-equal (passed) │ 3559.100155 │
511
+ │ deep-equal (passed) │ 3819.218852 │
492
512
  └────────────────────────────────────────┴────────────────┘
493
513
  ```
494
514
 
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
4
- const { hasOwnProperty } = Object.prototype;
4
+ const { hasOwnProperty, toString: toString$1 } = Object.prototype;
5
+ const OBJECT_TAG = '[object Object]';
5
6
  /**
6
7
  * Combine two comparators into a single comparators.
7
8
  */
@@ -50,6 +51,22 @@ function getStrictProperties(object) {
50
51
  const hasOwn =
51
52
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
52
53
  Object.hasOwn || ((object, property) => hasOwnProperty.call(object, property));
54
+ /**
55
+ * Whether a null-prototype object is being compared with a plain object. Only one side can be
56
+ * null-prototype when the constructors differ. The other side must be a plain object by
57
+ * constructor, or by tag when it comes from another realm.
58
+ */
59
+ function isNullPrototypeComparable(a, b) {
60
+ const aConstructor = a.constructor;
61
+ const bConstructor = b.constructor;
62
+ if (aConstructor == null) {
63
+ return bConstructor === Object || toString$1.call(b) === OBJECT_TAG;
64
+ }
65
+ if (bConstructor == null) {
66
+ return aConstructor === Object || toString$1.call(a) === OBJECT_TAG;
67
+ }
68
+ return false;
69
+ }
53
70
 
54
71
  const PREACT_VNODE = '__v';
55
72
  const PREACT_OWNER = '__o';
@@ -378,13 +395,42 @@ function isPropertyEqual(a, b, state, property) {
378
395
  return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
379
396
  }
380
397
 
381
- const toString = Object.prototype.toString;
398
+ const { toString } = Object.prototype;
382
399
  /**
383
400
  * Create a comparator method based on the type-specific equality comparators passed.
384
401
  */
385
402
  function createEqualityComparator(config) {
386
403
  const supportedComparatorMap = createSupportedComparatorMap(config);
387
- const { areArraysEqual, areDatesEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, areRegExpsEqual, areSetsEqual, getUnsupportedCustomComparator, } = config;
404
+ const { areArraysEqual, areDatesEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, areRegExpsEqual, areSetsEqual, getUnsupportedCustomComparator, strictNullPrototypeComparison, } = config;
405
+ /**
406
+ * Whether the custom objects are equal in value, based on their string tag. If not matching any tags
407
+ * that require a specific type of comparison, then we hard-code false because the only thing remaining
408
+ * is strict equality, which has already been compared. This is for a few reasons:
409
+ * - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
410
+ * comparison that can be made.
411
+ * - For types that can be introspected but do not have an objective definition of what
412
+ * equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
413
+ * In all cases, these decisions should be reevaluated based on changes to the language and
414
+ * common development practices.
415
+ *
416
+ * Capturing the tag is reasonably performant in modern environments like v8 and SpiderMonkey. This is
417
+ * kept out of the main comparator so that it stays within the engine's bytecode budget for inlining.
418
+ * See [#207](https://github.com/planttheidea/fast-equals/pull/207) for more details.
419
+ */
420
+ const areTaggedObjectsEqual = getUnsupportedCustomComparator
421
+ ? function areTaggedObjectsEqual(a, b, state) {
422
+ const tag = toString.call(a);
423
+ const supportedComparator = supportedComparatorMap[tag];
424
+ if (supportedComparator) {
425
+ return supportedComparator(a, b, state);
426
+ }
427
+ const unsupportedCustomComparator = getUnsupportedCustomComparator(a, b, state, tag);
428
+ return unsupportedCustomComparator ? unsupportedCustomComparator(a, b, state) : false;
429
+ }
430
+ : function areTaggedObjectsEqual(a, b, state) {
431
+ const supportedComparator = supportedComparatorMap[toString.call(a)];
432
+ return supportedComparator ? supportedComparator(a, b, state) : false;
433
+ };
388
434
  /**
389
435
  * compare the value of the two objects and return true if they are equivalent in values
390
436
  */
@@ -424,7 +470,7 @@ function createEqualityComparator(config) {
424
470
  // Constructors should match, otherwise there is potential for false positives
425
471
  // between class and subclass or custom object and POJO.
426
472
  if (constructor !== b.constructor) {
427
- return false;
473
+ return strictNullPrototypeComparison ? false : isNullPrototypeComparable(a, b) && areObjectsEqual(a, b, state);
428
474
  }
429
475
  // Try to fast-path equality checks for other complex object types in the
430
476
  // same realm to avoid capturing the string tag. Strict equality is used
@@ -463,32 +509,14 @@ function createEqualityComparator(config) {
463
509
  if (Array.isArray(a)) {
464
510
  return areArraysEqual(a, b, state);
465
511
  }
466
- // Since this is a custom object, capture the string tag to determining its type.
467
- // This is reasonably performant in modern environments like v8 and SpiderMonkey.
468
- const tag = toString.call(a);
469
- const supportedComparator = supportedComparatorMap[tag];
470
- if (supportedComparator) {
471
- return supportedComparator(a, b, state);
472
- }
473
- const unsupportedCustomComparator = getUnsupportedCustomComparator && getUnsupportedCustomComparator(a, b, state, tag);
474
- if (unsupportedCustomComparator) {
475
- return unsupportedCustomComparator(a, b, state);
476
- }
477
- // If not matching any tags that require a specific type of comparison, then we hard-code false because
478
- // the only thing remaining is strict equality, which has already been compared. This is for a few reasons:
479
- // - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
480
- // comparison that can be made.
481
- // - For types that can be introspected but do not have an objective definition of what
482
- // equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
483
- // In all cases, these decisions should be reevaluated based on changes to the language and
484
- // common development practices.
485
- return false;
512
+ // Since this is a custom object, use the string tag to determine its type.
513
+ return areTaggedObjectsEqual(a, b, state);
486
514
  };
487
515
  }
488
516
  /**
489
517
  * Create the configuration object used for building comparators.
490
518
  */
491
- function createEqualityComparatorConfig({ circular, createCustomConfig, strict, }) {
519
+ function createEqualityComparatorConfig({ circular, createCustomConfig, strict, strictNullPrototypeComparison = true, }) {
492
520
  let config = {
493
521
  areArrayBuffersEqual,
494
522
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
@@ -513,6 +541,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
513
541
  : areTypedArraysEqual,
514
542
  areUrlsEqual: areUrlsEqual,
515
543
  getUnsupportedCustomComparator: undefined,
544
+ strictNullPrototypeComparison,
516
545
  };
517
546
  if (createCustomConfig) {
518
547
  config = Object.assign({}, config, createCustomConfig(config));
@@ -94,6 +94,12 @@ interface ComparatorConfig<Meta> {
94
94
  * Get a custom comparator based on the objects passed.
95
95
  */
96
96
  getUnsupportedCustomComparator: ((a: any, b: any, state: State<Meta>, tag: string) => EqualityComparator<Meta> | undefined) | undefined;
97
+ /**
98
+ * Whether a null-prototype object (`Object.create(null)`) is unequal to an otherwise identical
99
+ * plain object. Defaults to `true`; when `false`, the two are compared as dictionaries.
100
+ * Constructor checks for all other types are unaffected.
101
+ */
102
+ strictNullPrototypeComparison?: boolean;
97
103
  }
98
104
  type EqualityComparator<Meta> = (a: any, b: any, state: State<Meta>) => boolean;
99
105
  type InternalEqualityComparator<Meta> = (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
@@ -138,6 +144,12 @@ interface CustomEqualCreatorOptions<Meta> {
138
144
  * with equal shape of descriptors.
139
145
  */
140
146
  strict?: boolean;
147
+ /**
148
+ * Whether a null-prototype object (`Object.create(null)`) is unequal to an otherwise identical
149
+ * plain object. Defaults to `true`; when `false`, the two are compared as dictionaries.
150
+ * Constructor checks for all other types are unaffected.
151
+ */
152
+ strictNullPrototypeComparison?: boolean;
141
153
  }
142
154
 
143
155
  /**
@@ -94,6 +94,12 @@ interface ComparatorConfig<Meta> {
94
94
  * Get a custom comparator based on the objects passed.
95
95
  */
96
96
  getUnsupportedCustomComparator: ((a: any, b: any, state: State<Meta>, tag: string) => EqualityComparator<Meta> | undefined) | undefined;
97
+ /**
98
+ * Whether a null-prototype object (`Object.create(null)`) is unequal to an otherwise identical
99
+ * plain object. Defaults to `true`; when `false`, the two are compared as dictionaries.
100
+ * Constructor checks for all other types are unaffected.
101
+ */
102
+ strictNullPrototypeComparison?: boolean;
97
103
  }
98
104
  type EqualityComparator<Meta> = (a: any, b: any, state: State<Meta>) => boolean;
99
105
  type InternalEqualityComparator<Meta> = (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
@@ -138,6 +144,12 @@ interface CustomEqualCreatorOptions<Meta> {
138
144
  * with equal shape of descriptors.
139
145
  */
140
146
  strict?: boolean;
147
+ /**
148
+ * Whether a null-prototype object (`Object.create(null)`) is unequal to an otherwise identical
149
+ * plain object. Defaults to `true`; when `false`, the two are compared as dictionaries.
150
+ * Constructor checks for all other types are unaffected.
151
+ */
152
+ strictNullPrototypeComparison?: boolean;
141
153
  }
142
154
 
143
155
  /**
package/dist/es/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
2
- const { hasOwnProperty } = Object.prototype;
2
+ const { hasOwnProperty, toString: toString$1 } = Object.prototype;
3
+ const OBJECT_TAG = '[object Object]';
3
4
  /**
4
5
  * Combine two comparators into a single comparators.
5
6
  */
@@ -48,6 +49,22 @@ function getStrictProperties(object) {
48
49
  const hasOwn =
49
50
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
50
51
  Object.hasOwn || ((object, property) => hasOwnProperty.call(object, property));
52
+ /**
53
+ * Whether a null-prototype object is being compared with a plain object. Only one side can be
54
+ * null-prototype when the constructors differ. The other side must be a plain object by
55
+ * constructor, or by tag when it comes from another realm.
56
+ */
57
+ function isNullPrototypeComparable(a, b) {
58
+ const aConstructor = a.constructor;
59
+ const bConstructor = b.constructor;
60
+ if (aConstructor == null) {
61
+ return bConstructor === Object || toString$1.call(b) === OBJECT_TAG;
62
+ }
63
+ if (bConstructor == null) {
64
+ return aConstructor === Object || toString$1.call(a) === OBJECT_TAG;
65
+ }
66
+ return false;
67
+ }
51
68
 
52
69
  const PREACT_VNODE = '__v';
53
70
  const PREACT_OWNER = '__o';
@@ -376,13 +393,42 @@ function isPropertyEqual(a, b, state, property) {
376
393
  return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
377
394
  }
378
395
 
379
- const toString = Object.prototype.toString;
396
+ const { toString } = Object.prototype;
380
397
  /**
381
398
  * Create a comparator method based on the type-specific equality comparators passed.
382
399
  */
383
400
  function createEqualityComparator(config) {
384
401
  const supportedComparatorMap = createSupportedComparatorMap(config);
385
- const { areArraysEqual, areDatesEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, areRegExpsEqual, areSetsEqual, getUnsupportedCustomComparator, } = config;
402
+ const { areArraysEqual, areDatesEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, areRegExpsEqual, areSetsEqual, getUnsupportedCustomComparator, strictNullPrototypeComparison, } = config;
403
+ /**
404
+ * Whether the custom objects are equal in value, based on their string tag. If not matching any tags
405
+ * that require a specific type of comparison, then we hard-code false because the only thing remaining
406
+ * is strict equality, which has already been compared. This is for a few reasons:
407
+ * - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
408
+ * comparison that can be made.
409
+ * - For types that can be introspected but do not have an objective definition of what
410
+ * equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
411
+ * In all cases, these decisions should be reevaluated based on changes to the language and
412
+ * common development practices.
413
+ *
414
+ * Capturing the tag is reasonably performant in modern environments like v8 and SpiderMonkey. This is
415
+ * kept out of the main comparator so that it stays within the engine's bytecode budget for inlining.
416
+ * See [#207](https://github.com/planttheidea/fast-equals/pull/207) for more details.
417
+ */
418
+ const areTaggedObjectsEqual = getUnsupportedCustomComparator
419
+ ? function areTaggedObjectsEqual(a, b, state) {
420
+ const tag = toString.call(a);
421
+ const supportedComparator = supportedComparatorMap[tag];
422
+ if (supportedComparator) {
423
+ return supportedComparator(a, b, state);
424
+ }
425
+ const unsupportedCustomComparator = getUnsupportedCustomComparator(a, b, state, tag);
426
+ return unsupportedCustomComparator ? unsupportedCustomComparator(a, b, state) : false;
427
+ }
428
+ : function areTaggedObjectsEqual(a, b, state) {
429
+ const supportedComparator = supportedComparatorMap[toString.call(a)];
430
+ return supportedComparator ? supportedComparator(a, b, state) : false;
431
+ };
386
432
  /**
387
433
  * compare the value of the two objects and return true if they are equivalent in values
388
434
  */
@@ -422,7 +468,7 @@ function createEqualityComparator(config) {
422
468
  // Constructors should match, otherwise there is potential for false positives
423
469
  // between class and subclass or custom object and POJO.
424
470
  if (constructor !== b.constructor) {
425
- return false;
471
+ return strictNullPrototypeComparison ? false : isNullPrototypeComparable(a, b) && areObjectsEqual(a, b, state);
426
472
  }
427
473
  // Try to fast-path equality checks for other complex object types in the
428
474
  // same realm to avoid capturing the string tag. Strict equality is used
@@ -461,32 +507,14 @@ function createEqualityComparator(config) {
461
507
  if (Array.isArray(a)) {
462
508
  return areArraysEqual(a, b, state);
463
509
  }
464
- // Since this is a custom object, capture the string tag to determining its type.
465
- // This is reasonably performant in modern environments like v8 and SpiderMonkey.
466
- const tag = toString.call(a);
467
- const supportedComparator = supportedComparatorMap[tag];
468
- if (supportedComparator) {
469
- return supportedComparator(a, b, state);
470
- }
471
- const unsupportedCustomComparator = getUnsupportedCustomComparator && getUnsupportedCustomComparator(a, b, state, tag);
472
- if (unsupportedCustomComparator) {
473
- return unsupportedCustomComparator(a, b, state);
474
- }
475
- // If not matching any tags that require a specific type of comparison, then we hard-code false because
476
- // the only thing remaining is strict equality, which has already been compared. This is for a few reasons:
477
- // - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
478
- // comparison that can be made.
479
- // - For types that can be introspected but do not have an objective definition of what
480
- // equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
481
- // In all cases, these decisions should be reevaluated based on changes to the language and
482
- // common development practices.
483
- return false;
510
+ // Since this is a custom object, use the string tag to determine its type.
511
+ return areTaggedObjectsEqual(a, b, state);
484
512
  };
485
513
  }
486
514
  /**
487
515
  * Create the configuration object used for building comparators.
488
516
  */
489
- function createEqualityComparatorConfig({ circular, createCustomConfig, strict, }) {
517
+ function createEqualityComparatorConfig({ circular, createCustomConfig, strict, strictNullPrototypeComparison = true, }) {
490
518
  let config = {
491
519
  areArrayBuffersEqual,
492
520
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
@@ -511,6 +539,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
511
539
  : areTypedArraysEqual,
512
540
  areUrlsEqual: areUrlsEqual,
513
541
  getUnsupportedCustomComparator: undefined,
542
+ strictNullPrototypeComparison,
514
543
  };
515
544
  if (createCustomConfig) {
516
545
  config = Object.assign({}, config, createCustomConfig(config));
package/index.d.ts CHANGED
@@ -94,6 +94,12 @@ interface ComparatorConfig<Meta> {
94
94
  * Get a custom comparator based on the objects passed.
95
95
  */
96
96
  getUnsupportedCustomComparator: ((a: any, b: any, state: State<Meta>, tag: string) => EqualityComparator<Meta> | undefined) | undefined;
97
+ /**
98
+ * Whether a null-prototype object (`Object.create(null)`) is unequal to an otherwise identical
99
+ * plain object. Defaults to `true`; when `false`, the two are compared as dictionaries.
100
+ * Constructor checks for all other types are unaffected.
101
+ */
102
+ strictNullPrototypeComparison?: boolean;
97
103
  }
98
104
  type EqualityComparator<Meta> = (a: any, b: any, state: State<Meta>) => boolean;
99
105
  type InternalEqualityComparator<Meta> = (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
@@ -138,6 +144,12 @@ interface CustomEqualCreatorOptions<Meta> {
138
144
  * with equal shape of descriptors.
139
145
  */
140
146
  strict?: boolean;
147
+ /**
148
+ * Whether a null-prototype object (`Object.create(null)`) is unequal to an otherwise identical
149
+ * plain object. Defaults to `true`; when `false`, the two are compared as dictionaries.
150
+ * Constructor checks for all other types are unaffected.
151
+ */
152
+ strictNullPrototypeComparison?: boolean;
141
153
  }
142
154
 
143
155
  /**
package/package.json CHANGED
@@ -110,5 +110,5 @@
110
110
  "sideEffects": false,
111
111
  "type": "module",
112
112
  "types": "./index.d.ts",
113
- "version": "6.0.4"
113
+ "version": "6.1.0"
114
114
  }