fast-equals 6.4.0 → 6.4.2

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.90kB when minified and gzipped.
4
+ various use-cases. It has no dependencies, and is ~3.06kB when minified and gzipped.
5
5
 
6
6
  The following types are handled out-of-the-box:
7
7
 
@@ -36,6 +36,7 @@ If you are having any problems, want to request a new feature, or have any quest
36
36
  - [Available methods](#available-methods)
37
37
  - [deepEqual](#deepequal)
38
38
  - [Comparing `Map`s](#comparing-maps)
39
+ - [Comparing `URL`s and `URLSearchParams`](#comparing-urls-and-urlsearchparams)
39
40
  - [shallowEqual](#shallowequal)
40
41
  - [sameValueEqual](#samevalueequal)
41
42
  - [sameValueZeroEqual](#samevaluezeroequal)
@@ -93,6 +94,20 @@ deepEqual(mapA, mapB);
93
94
  To support true deep equality of all contents, `fast-equals` will perform a deep equality comparison for key and value
94
95
  pairs. Therefore, the above would be `true`.
95
96
 
97
+ #### Comparing `URL`s and `URLSearchParams`
98
+
99
+ `URL` objects are equal when their `href` matches, as in the
100
+ [URL Standard](https://url.spec.whatwg.org/#url-equivalence), except that query pair order is not significant (repeated
101
+ keys are counted). Everything else is compared as written:
102
+
103
+ ```ts
104
+ deepEqual(new URL('https://foo.com/?a=1&b=2'), new URL('https://foo.com/?b=2&a=1')); // true
105
+ deepEqual(new URL('https://foo.com/?a=%41'), new URL('https://foo.com/?a=A')); // false
106
+ ```
107
+
108
+ `URLSearchParams` objects follow the same rule, but they only keep the decoded pairs, so the `searchParams` of the two
109
+ URLs above are equal. Equal `URL`s always have equal `searchParams`, but not the reverse.
110
+
96
111
  ### shallowEqual
97
112
 
98
113
  Performs a shallow equality comparison on the two objects passed and returns a boolean representing the value
@@ -460,74 +475,74 @@ Testing mixed objects equal...
460
475
  ┌────────────────────────────────────────┬────────────────┐
461
476
  │ Name │ Ops / sec │
462
477
  ├────────────────────────────────────────┼────────────────┤
463
- │ fast-equals (passed) │ 1731598.023836 │
478
+ │ fast-equals (passed) │ 1782450.451369 │
464
479
  ├────────────────────────────────────────┼────────────────┤
465
- │ fast-deep-equal (passed) │ 1309307.899547 │
480
+ │ fast-deep-equal (passed) │ 1318966.05525 │
466
481
  ├────────────────────────────────────────┼────────────────┤
467
- │ react-fast-compare (passed) │ 1292046.683991 │
482
+ │ react-fast-compare (passed) │ 1312506.681929 │
468
483
  ├────────────────────────────────────────┼────────────────┤
469
- │ shallow-equal-fuzzy (passed) │ 1226265.458405 │
484
+ │ shallow-equal-fuzzy (passed) │ 1278565.812104 │
470
485
  ├────────────────────────────────────────┼────────────────┤
471
- │ nano-equal (failed) │ 963157.128972 │
486
+ │ nano-equal (failed) │ 983769.888387 │
472
487
  ├────────────────────────────────────────┼────────────────┤
473
- │ fast-equals (circular) (passed) │ 909479.445346 │
488
+ │ fast-equals (circular) (passed) │ 907118.190745 │
474
489
  ├────────────────────────────────────────┼────────────────┤
475
- │ dequal/lite (passed) │ 778122.31992 │
490
+ │ dequal/lite (passed) │ 783946.658853 │
476
491
  ├────────────────────────────────────────┼────────────────┤
477
- │ dequal (passed) │ 760245.363659 │
492
+ │ dequal (passed) │ 765217.048928 │
478
493
  ├────────────────────────────────────────┼────────────────┤
479
- │ underscore.isEqual (passed) │ 445304.799867 │
494
+ │ underscore.isEqual (passed) │ 461845.668653 │
480
495
  ├────────────────────────────────────────┼────────────────┤
481
- │ assert.deepStrictEqual (passed) │ 438737.156408 │
496
+ │ assert.deepStrictEqual (passed) │ 440373.095029 │
482
497
  ├────────────────────────────────────────┼────────────────┤
483
- │ es-toolkit (passed) │ 382792.590707 │
498
+ │ es-toolkit (passed) │ 391305.456936 │
484
499
  ├────────────────────────────────────────┼────────────────┤
485
- │ lodash.isEqual (passed) │ 290229.549964 │
500
+ │ lodash.isEqual (passed) │ 290278.80479 │
486
501
  ├────────────────────────────────────────┼────────────────┤
487
- │ fast-equals (strict) (passed) │ 279185.538603 │
502
+ │ fast-equals (strict) (passed) │ 290096.328199 │
488
503
  ├────────────────────────────────────────┼────────────────┤
489
- │ fast-equals (strict circular) (passed) │ 238470.634046 │
504
+ │ fast-equals (strict circular) (passed) │ 246541.923525 │
490
505
  ├────────────────────────────────────────┼────────────────┤
491
- │ deep-eql (passed) │ 172670.983717 │
506
+ │ deep-eql (passed) │ 173208.792462 │
492
507
  ├────────────────────────────────────────┼────────────────┤
493
- │ deep-equal (passed) │ 1357.008406 │
508
+ │ deep-equal (passed) │ 1412.586981 │
494
509
  └────────────────────────────────────────┴────────────────┘
495
510
 
496
511
  Testing mixed objects not equal...
497
512
  ┌────────────────────────────────────────┬────────────────┐
498
513
  │ Name │ Ops / sec │
499
514
  ├────────────────────────────────────────┼────────────────┤
500
- │ fast-equals (passed) │ 5076106.125712 │
515
+ │ fast-equals (passed) │ 5488976.412437 │
501
516
  ├────────────────────────────────────────┼────────────────┤
502
- │ fast-equals (circular) (passed) │ 3544911.437592 │
517
+ │ fast-equals (circular) (passed) │ 3668925.717479 │
503
518
  ├────────────────────────────────────────┼────────────────┤
504
- │ fast-deep-equal (passed) │ 3433830.73191 │
519
+ │ fast-deep-equal (passed) │ 3552737.084766 │
505
520
  ├────────────────────────────────────────┼────────────────┤
506
- │ react-fast-compare (passed) │ 3246695.251508 │
521
+ │ react-fast-compare (passed) │ 3441428.035043 │
507
522
  ├────────────────────────────────────────┼────────────────┤
508
- │ fast-equals (strict) (passed) │ 1584702.069444 │
523
+ │ fast-equals (strict) (passed) │ 1668916.622434 │
509
524
  ├────────────────────────────────────────┼────────────────┤
510
- │ dequal/lite (passed) │ 1393178.232807 │
525
+ │ dequal/lite (passed) │ 1446091.459485 │
511
526
  ├────────────────────────────────────────┼────────────────┤
512
- │ dequal (passed) │ 1370661.947753 │
527
+ │ fast-equals (strict circular) (passed) │ 1410439.853116 │
513
528
  ├────────────────────────────────────────┼────────────────┤
514
- │ fast-equals (strict circular) (passed) │ 1336500.602356 │
529
+ │ dequal (passed) │ 1395740.020934 │
515
530
  ├────────────────────────────────────────┼────────────────┤
516
- │ shallow-equal-fuzzy (failed) │ 1219389.24268 │
531
+ │ shallow-equal-fuzzy (failed) │ 1285610.296881 │
517
532
  ├────────────────────────────────────────┼────────────────┤
518
- │ nano-equal (passed) │ 1088061.461684 │
533
+ │ nano-equal (passed) │ 1130229.996399 │
519
534
  ├────────────────────────────────────────┼────────────────┤
520
- │ underscore.isEqual (passed) │ 661655.436986 │
535
+ │ underscore.isEqual (passed) │ 682455.329594 │
521
536
  ├────────────────────────────────────────┼────────────────┤
522
- │ es-toolkit (passed) │ 401295.827671 │
537
+ │ es-toolkit (passed) │ 417584.470986 │
523
538
  ├────────────────────────────────────────┼────────────────┤
524
- │ lodash.isEqual (passed) │ 377347.276362 │
539
+ │ lodash.isEqual (passed) │ 385904.998549 │
525
540
  ├────────────────────────────────────────┼────────────────┤
526
- │ deep-eql (passed) │ 199704.751472 │
541
+ │ deep-eql (passed) │ 202212.291682 │
527
542
  ├────────────────────────────────────────┼────────────────┤
528
- │ assert.deepStrictEqual (passed) │ 25504.609914 │
543
+ │ assert.deepStrictEqual (passed) │ 25540.74079 │
529
544
  ├────────────────────────────────────────┼────────────────┤
530
- │ deep-equal (passed) │ 5996.790087 │
545
+ │ deep-equal (passed) │ 5867.29696 │
531
546
  └────────────────────────────────────────┴────────────────┘
532
547
  ```
533
548
 
@@ -2,7 +2,6 @@
2
2
 
3
3
  const { getOwnPropertyDescriptor: getOwnPropertyDescriptor$1, getOwnPropertyNames, getOwnPropertySymbols } = Object;
4
4
  const { hasOwnProperty, toString: toString$1 } = Object.prototype;
5
- const OBJECT_TAG = '[object Object]';
6
5
  /**
7
6
  * Whether the own properties from `end` onward are equal with strict property checking, where the
8
7
  * caller has already verified that both objects have the same number of own properties.
@@ -134,10 +133,10 @@ function isNullPrototypeComparable(a, b) {
134
133
  const aConstructor = a.constructor;
135
134
  const bConstructor = b.constructor;
136
135
  if (aConstructor == null) {
137
- return bConstructor === Object || toString$1.call(b) === OBJECT_TAG;
136
+ return bConstructor === Object || toString$1.call(b) === '[object Object]';
138
137
  }
139
138
  if (bConstructor == null) {
140
- return aConstructor === Object || toString$1.call(a) === OBJECT_TAG;
139
+ return aConstructor === Object || toString$1.call(a) === '[object Object]';
141
140
  }
142
141
  return false;
143
142
  }
@@ -154,15 +153,32 @@ function isReactProperty(a, b, property) {
154
153
  (property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
155
154
  }
156
155
  /**
157
- * Reorder a serialized query string so that params holding the same pairs compare as equal
158
- * regardless of the order they appear in.
156
+ * Whether two serialized query strings hold the same pairs, regardless of the order they appear in.
159
157
  *
160
158
  * @note
161
- * The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
162
- * splitting on `&` recovers exactly the pairs and nothing else.
159
+ * Every `&` separates pairs, so splitting on it recovers exactly the pairs and nothing else. This
160
+ * holds for both inputs this receives: `URLSearchParams` serialization percent-encodes `&` inside
161
+ * a name or a value, and in a URL's query as written the parser treats every `&` as a separator.
162
+ * Empty pairs (from `a=1&` or `a=1&&b=2`) are kept, so they count like any other pair.
163
+ *
164
+ * Callers only reach this once they know the strings differ, so it does not check `===` first.
163
165
  */
164
- function sortSearchParams(serialized) {
165
- return serialized.split('&').sort().join('&');
166
+ function areSortedPairsEqual(serializedA, serializedB) {
167
+ const pairsA = serializedA.split('&');
168
+ const pairsB = serializedB.split('&');
169
+ const length = pairsA.length;
170
+ if (length !== pairsB.length) {
171
+ return false;
172
+ }
173
+ pairsA.sort();
174
+ pairsB.sort();
175
+ // Comparing pair by pair stops at the first difference, rather than joining both back together.
176
+ for (let index = 0; index < length; ++index) {
177
+ if (pairsA[index] !== pairsB[index]) {
178
+ return false;
179
+ }
180
+ }
181
+ return true;
166
182
  }
167
183
 
168
184
  /**
@@ -491,13 +507,20 @@ function areRegExpsEqualStrict(a, b, state) {
491
507
  * Order is not significant, matching how the other unordered collections in the library are
492
508
  * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
493
509
  * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
510
+ *
511
+ * This is the same rule `areUrlsEqual` applies to a URL's query, applied to the params'
512
+ * serialization. `URLSearchParams` keeps only the decoded pairs, so differences in how a query was
513
+ * written (`a=%41` vs `a=A`, `a` vs `a=`, a trailing `&`) are gone before they reach here. As a
514
+ * result, URLs that are equal always have equal `searchParams`, but not the reverse.
494
515
  */
495
516
  function areUrlSearchParamsEqual(a, b) {
496
517
  const serializedA = a.toString();
497
518
  const serializedB = b.toString();
498
519
  // Identical serializations are equal under any ordering, and this is by far the common case, so
499
- // it is worth checking before sorting anything.
500
- return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
520
+ // it is worth checking before sorting anything. Reordering pairs preserves length, so a length
521
+ // mismatch rules out equality without sorting either.
522
+ return (serializedA === serializedB
523
+ || (serializedA.length === serializedB.length && areSortedPairsEqual(serializedA, serializedB)));
501
524
  }
502
525
  /**
503
526
  * Whether the search params passed are equal in value with strict property checking.
@@ -603,19 +626,43 @@ function areTypedArraysEqualStrict(a, b, state) {
603
626
  * Whether the URL instances are equal in value.
604
627
  */
605
628
  function areUrlsEqual(a, b) {
629
+ const hrefA = a.href;
630
+ const hrefB = b.href;
606
631
  // `href` is the normalized serialization of every component, so matching hrefs are equal without
607
632
  // any further work. Only a difference in query parameter ordering can survive a mismatch here.
608
- if (a.href === b.href) {
633
+ if (hrefA === hrefB) {
609
634
  return true;
610
635
  }
611
- return (a.protocol === b.protocol
612
- && a.username === b.username
613
- && a.password === b.password
614
- // `host` covers both the hostname and the port.
615
- && a.host === b.host
616
- && a.pathname === b.pathname
617
- && a.hash === b.hash
618
- && areUrlSearchParamsEqual(a.searchParams, b.searchParams));
636
+ // Reordering the query's pairs preserves length, so equal URLs always have hrefs of equal length.
637
+ const length = hrefA.length;
638
+ if (length !== hrefB.length) {
639
+ return false;
640
+ }
641
+ // The rest of the URL is compared as serialized, rather than through the component getters, which
642
+ // hide some differences (`search` and `hash` are `''` for both `?` / `#` and a missing query /
643
+ // fragment). Every component before the query percent-encodes `?`, so the first one starts the
644
+ // query, or the fragment when there is no query.
645
+ const start = hrefA.indexOf('?');
646
+ if (start === -1 || start !== hrefB.indexOf('?') || hrefA.slice(0, start) !== hrefB.slice(0, start)) {
647
+ return false;
648
+ }
649
+ // The query percent-encodes `#`, so the first one starts the fragment. One before the `?` means
650
+ // that `?` is inside the fragment, so there is no query to reorder. The prefixes match, so that
651
+ // `#` is in both hrefs.
652
+ let end = hrefA.indexOf('#');
653
+ if (end === -1) {
654
+ if (hrefB.includes('#', start)) {
655
+ return false;
656
+ }
657
+ end = length;
658
+ }
659
+ else if (end < start || hrefB.indexOf('#', start) !== end || hrefA.slice(end) !== hrefB.slice(end)) {
660
+ return false;
661
+ }
662
+ const queryA = hrefA.slice(start + 1, end);
663
+ // Everything outside the query matches and the hrefs differ, so the queries differ, and a query
664
+ // with a single pair cannot be reordered.
665
+ return queryA.includes('&') && areSortedPairsEqual(queryA, hrefB.slice(start + 1, end));
619
666
  }
620
667
  /**
621
668
  * Whether the URL instances are equal in value with strict property checking.
@@ -625,6 +672,7 @@ function areUrlsEqualStrict(a, b, state) {
625
672
  }
626
673
 
627
674
  const { toString } = Object.prototype;
675
+ const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;
628
676
  /**
629
677
  * Create a comparator method based on the type-specific equality comparators passed.
630
678
  */
@@ -642,22 +690,30 @@ function createEqualityComparator(config) {
642
690
  * In all cases, these decisions should be reevaluated based on changes to the language and
643
691
  * common development practices.
644
692
  *
693
+ * When `Symbol.toStringTag` is a non-empty string (TypedArrays, `ArrayBuffer`, `DataView`, `Map`, `URL`,
694
+ * etc.), `toString()` returns `[object ${stringTag}]` regardless of the object's type, so the comparator
695
+ * is looked up by the `Symbol.toStringTag` value directly. This avoids building (and then hashing) the
696
+ * full tag string for every comparison. Otherwise the tag depends on the built-in type, and is captured
697
+ * with `toString()` as before.
698
+ *
645
699
  * Capturing the tag is reasonably performant in modern environments like v8 and SpiderMonkey. This is
646
700
  * kept out of the main comparator so that it stays within the engine's bytecode budget for inlining.
647
701
  * See [#207](https://github.com/planttheidea/fast-equals/pull/207) for more details.
648
702
  */
649
703
  const areTaggedObjectsEqual = getUnsupportedCustomComparator
650
704
  ? function areTaggedObjectsEqual(a, b, state) {
651
- const tag = toString.call(a);
705
+ const stringTag = toStringTag && a[toStringTag];
706
+ const tag = stringTag && typeof stringTag === 'string' ? stringTag : toString.call(a);
652
707
  const supportedComparator = supportedComparatorMap[tag];
653
708
  if (supportedComparator) {
654
709
  return supportedComparator(a, b, state);
655
710
  }
656
- const unsupportedCustomComparator = getUnsupportedCustomComparator(a, b, state, tag);
711
+ const unsupportedCustomComparator = getUnsupportedCustomComparator(a, b, state, tag === stringTag ? toString.call(a) : tag);
657
712
  return unsupportedCustomComparator ? unsupportedCustomComparator(a, b, state) : false;
658
713
  }
659
714
  : function areTaggedObjectsEqual(a, b, state) {
660
- const supportedComparator = supportedComparatorMap[toString.call(a)];
715
+ const stringTag = toStringTag && a[toStringTag];
716
+ const supportedComparator = supportedComparatorMap[stringTag && typeof stringTag === 'string' ? stringTag : toString.call(a)];
661
717
  return supportedComparator ? supportedComparator(a, b, state) : false;
662
718
  };
663
719
  /**
@@ -837,50 +893,61 @@ function createIsEqual({ circular, comparator, createState, equals, strict }) {
837
893
  };
838
894
  }
839
895
  /**
840
- * Create a map of `toString()` values to their respective handlers for `tag`-based lookups.
841
- */
842
- function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlSearchParamsEqual, areUrlsEqual, }) {
843
- return {
844
- '[object Arguments]': areObjectsEqual,
845
- '[object Array]': areArraysEqual,
846
- '[object ArrayBuffer]': areArrayBuffersEqual,
847
- '[object AsyncGeneratorFunction]': areFunctionsEqual,
848
- '[object BigInt]': arePrimitiveWrappersEqual,
849
- '[object BigInt64Array]': areTypedArraysEqual,
850
- '[object BigUint64Array]': areTypedArraysEqual,
851
- '[object Boolean]': arePrimitiveWrappersEqual,
852
- '[object DataView]': areDataViewsEqual,
853
- '[object Date]': areDatesEqual,
896
+ * Create a map of tags to their respective handlers for `tag`-based lookups. Each handler is keyed
897
+ * by both its `Symbol.toStringTag` value (`Map`) and its `toString()` value (`[object Map]`).
898
+ *
899
+ * @note
900
+ * The map has a `null` prototype, so a `Symbol.toStringTag` like `constructor` or `toString` cannot
901
+ * resolve to an inherited `Object.prototype` method.
902
+ */
903
+ function createSupportedComparatorMap(config) {
904
+ return [
905
+ ['Arguments', config.areObjectsEqual],
906
+ ['Array', config.areArraysEqual],
907
+ ['ArrayBuffer', config.areArrayBuffersEqual],
908
+ ['AsyncGeneratorFunction', config.areFunctionsEqual],
909
+ ['BigInt', config.arePrimitiveWrappersEqual],
910
+ ['BigInt64Array', config.areTypedArraysEqual],
911
+ ['BigUint64Array', config.areTypedArraysEqual],
912
+ ['Boolean', config.arePrimitiveWrappersEqual],
913
+ ['DataView', config.areDataViewsEqual],
914
+ ['Date', config.areDatesEqual],
854
915
  // If an error tag, it should be tested explicitly. Like RegExp, the properties are not
855
916
  // enumerable, and therefore will give false positives if tested like a standard object.
856
- '[object Error]': areErrorsEqual,
857
- '[object Float16Array]': areTypedArraysEqual,
858
- '[object Float32Array]': areTypedArraysEqual,
859
- '[object Float64Array]': areTypedArraysEqual,
860
- '[object Function]': areFunctionsEqual,
861
- '[object GeneratorFunction]': areFunctionsEqual,
862
- '[object Int8Array]': areTypedArraysEqual,
863
- '[object Int16Array]': areTypedArraysEqual,
864
- '[object Int32Array]': areTypedArraysEqual,
865
- '[object Map]': areMapsEqual,
866
- '[object Number]': arePrimitiveWrappersEqual,
867
- '[object Object]': (a, b, state) =>
868
- // The exception for value comparison is custom `Promise`-like class instances. These should
869
- // be treated the same as standard `Promise` objects, which means strict equality, and if
870
- // it reaches this point then that strict equality comparison has already failed.
871
- typeof a.then !== 'function' && typeof b.then !== 'function' && areObjectsEqual(a, b, state),
917
+ ['Error', config.areErrorsEqual],
918
+ ['Float16Array', config.areTypedArraysEqual],
919
+ ['Float32Array', config.areTypedArraysEqual],
920
+ ['Float64Array', config.areTypedArraysEqual],
921
+ ['Function', config.areFunctionsEqual],
922
+ ['GeneratorFunction', config.areFunctionsEqual],
923
+ ['Int8Array', config.areTypedArraysEqual],
924
+ ['Int16Array', config.areTypedArraysEqual],
925
+ ['Int32Array', config.areTypedArraysEqual],
926
+ ['Map', config.areMapsEqual],
927
+ ['Number', config.arePrimitiveWrappersEqual],
928
+ [
929
+ 'Object',
930
+ (a, b, state) =>
931
+ // The exception for value comparison is custom `Promise`-like class instances. These should
932
+ // be treated the same as standard `Promise` objects, which means strict equality, and if
933
+ // it reaches this point then that strict equality comparison has already failed.
934
+ typeof a.then !== 'function' && typeof b.then !== 'function' && config.areObjectsEqual(a, b, state),
935
+ ],
872
936
  // For RegExp, the properties are not enumerable, and therefore will give false positives if
873
937
  // tested like a standard object.
874
- '[object RegExp]': areRegExpsEqual,
875
- '[object Set]': areSetsEqual,
876
- '[object String]': arePrimitiveWrappersEqual,
877
- '[object URL]': areUrlsEqual,
878
- '[object URLSearchParams]': areUrlSearchParamsEqual,
879
- '[object Uint8Array]': areTypedArraysEqual,
880
- '[object Uint8ClampedArray]': areTypedArraysEqual,
881
- '[object Uint16Array]': areTypedArraysEqual,
882
- '[object Uint32Array]': areTypedArraysEqual,
883
- };
938
+ ['RegExp', config.areRegExpsEqual],
939
+ ['Set', config.areSetsEqual],
940
+ ['String', config.arePrimitiveWrappersEqual],
941
+ ['URL', config.areUrlsEqual],
942
+ ['URLSearchParams', config.areUrlSearchParamsEqual],
943
+ ['Uint8Array', config.areTypedArraysEqual],
944
+ ['Uint8ClampedArray', config.areTypedArraysEqual],
945
+ ['Uint16Array', config.areTypedArraysEqual],
946
+ ['Uint32Array', config.areTypedArraysEqual],
947
+ ].reduce((comparatorMap, [tag, comparator]) => {
948
+ comparatorMap[tag] = comparatorMap[`[object ${tag}]`] = comparator;
949
+ return comparatorMap;
950
+ }, Object.create(null));
884
951
  }
885
952
 
886
953
  /**
package/dist/es/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  const { getOwnPropertyDescriptor: getOwnPropertyDescriptor$1, getOwnPropertyNames, getOwnPropertySymbols } = Object;
2
2
  const { hasOwnProperty, toString: toString$1 } = Object.prototype;
3
- const OBJECT_TAG = '[object Object]';
4
3
  /**
5
4
  * Whether the own properties from `end` onward are equal with strict property checking, where the
6
5
  * caller has already verified that both objects have the same number of own properties.
@@ -132,10 +131,10 @@ function isNullPrototypeComparable(a, b) {
132
131
  const aConstructor = a.constructor;
133
132
  const bConstructor = b.constructor;
134
133
  if (aConstructor == null) {
135
- return bConstructor === Object || toString$1.call(b) === OBJECT_TAG;
134
+ return bConstructor === Object || toString$1.call(b) === '[object Object]';
136
135
  }
137
136
  if (bConstructor == null) {
138
- return aConstructor === Object || toString$1.call(a) === OBJECT_TAG;
137
+ return aConstructor === Object || toString$1.call(a) === '[object Object]';
139
138
  }
140
139
  return false;
141
140
  }
@@ -152,15 +151,32 @@ function isReactProperty(a, b, property) {
152
151
  (property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
153
152
  }
154
153
  /**
155
- * Reorder a serialized query string so that params holding the same pairs compare as equal
156
- * regardless of the order they appear in.
154
+ * Whether two serialized query strings hold the same pairs, regardless of the order they appear in.
157
155
  *
158
156
  * @note
159
- * The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
160
- * splitting on `&` recovers exactly the pairs and nothing else.
157
+ * Every `&` separates pairs, so splitting on it recovers exactly the pairs and nothing else. This
158
+ * holds for both inputs this receives: `URLSearchParams` serialization percent-encodes `&` inside
159
+ * a name or a value, and in a URL's query as written the parser treats every `&` as a separator.
160
+ * Empty pairs (from `a=1&` or `a=1&&b=2`) are kept, so they count like any other pair.
161
+ *
162
+ * Callers only reach this once they know the strings differ, so it does not check `===` first.
161
163
  */
162
- function sortSearchParams(serialized) {
163
- return serialized.split('&').sort().join('&');
164
+ function areSortedPairsEqual(serializedA, serializedB) {
165
+ const pairsA = serializedA.split('&');
166
+ const pairsB = serializedB.split('&');
167
+ const length = pairsA.length;
168
+ if (length !== pairsB.length) {
169
+ return false;
170
+ }
171
+ pairsA.sort();
172
+ pairsB.sort();
173
+ // Comparing pair by pair stops at the first difference, rather than joining both back together.
174
+ for (let index = 0; index < length; ++index) {
175
+ if (pairsA[index] !== pairsB[index]) {
176
+ return false;
177
+ }
178
+ }
179
+ return true;
164
180
  }
165
181
 
166
182
  /**
@@ -489,13 +505,20 @@ function areRegExpsEqualStrict(a, b, state) {
489
505
  * Order is not significant, matching how the other unordered collections in the library are
490
506
  * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
491
507
  * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
508
+ *
509
+ * This is the same rule `areUrlsEqual` applies to a URL's query, applied to the params'
510
+ * serialization. `URLSearchParams` keeps only the decoded pairs, so differences in how a query was
511
+ * written (`a=%41` vs `a=A`, `a` vs `a=`, a trailing `&`) are gone before they reach here. As a
512
+ * result, URLs that are equal always have equal `searchParams`, but not the reverse.
492
513
  */
493
514
  function areUrlSearchParamsEqual(a, b) {
494
515
  const serializedA = a.toString();
495
516
  const serializedB = b.toString();
496
517
  // Identical serializations are equal under any ordering, and this is by far the common case, so
497
- // it is worth checking before sorting anything.
498
- return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
518
+ // it is worth checking before sorting anything. Reordering pairs preserves length, so a length
519
+ // mismatch rules out equality without sorting either.
520
+ return (serializedA === serializedB
521
+ || (serializedA.length === serializedB.length && areSortedPairsEqual(serializedA, serializedB)));
499
522
  }
500
523
  /**
501
524
  * Whether the search params passed are equal in value with strict property checking.
@@ -601,19 +624,43 @@ function areTypedArraysEqualStrict(a, b, state) {
601
624
  * Whether the URL instances are equal in value.
602
625
  */
603
626
  function areUrlsEqual(a, b) {
627
+ const hrefA = a.href;
628
+ const hrefB = b.href;
604
629
  // `href` is the normalized serialization of every component, so matching hrefs are equal without
605
630
  // any further work. Only a difference in query parameter ordering can survive a mismatch here.
606
- if (a.href === b.href) {
631
+ if (hrefA === hrefB) {
607
632
  return true;
608
633
  }
609
- return (a.protocol === b.protocol
610
- && a.username === b.username
611
- && a.password === b.password
612
- // `host` covers both the hostname and the port.
613
- && a.host === b.host
614
- && a.pathname === b.pathname
615
- && a.hash === b.hash
616
- && areUrlSearchParamsEqual(a.searchParams, b.searchParams));
634
+ // Reordering the query's pairs preserves length, so equal URLs always have hrefs of equal length.
635
+ const length = hrefA.length;
636
+ if (length !== hrefB.length) {
637
+ return false;
638
+ }
639
+ // The rest of the URL is compared as serialized, rather than through the component getters, which
640
+ // hide some differences (`search` and `hash` are `''` for both `?` / `#` and a missing query /
641
+ // fragment). Every component before the query percent-encodes `?`, so the first one starts the
642
+ // query, or the fragment when there is no query.
643
+ const start = hrefA.indexOf('?');
644
+ if (start === -1 || start !== hrefB.indexOf('?') || hrefA.slice(0, start) !== hrefB.slice(0, start)) {
645
+ return false;
646
+ }
647
+ // The query percent-encodes `#`, so the first one starts the fragment. One before the `?` means
648
+ // that `?` is inside the fragment, so there is no query to reorder. The prefixes match, so that
649
+ // `#` is in both hrefs.
650
+ let end = hrefA.indexOf('#');
651
+ if (end === -1) {
652
+ if (hrefB.includes('#', start)) {
653
+ return false;
654
+ }
655
+ end = length;
656
+ }
657
+ else if (end < start || hrefB.indexOf('#', start) !== end || hrefA.slice(end) !== hrefB.slice(end)) {
658
+ return false;
659
+ }
660
+ const queryA = hrefA.slice(start + 1, end);
661
+ // Everything outside the query matches and the hrefs differ, so the queries differ, and a query
662
+ // with a single pair cannot be reordered.
663
+ return queryA.includes('&') && areSortedPairsEqual(queryA, hrefB.slice(start + 1, end));
617
664
  }
618
665
  /**
619
666
  * Whether the URL instances are equal in value with strict property checking.
@@ -623,6 +670,7 @@ function areUrlsEqualStrict(a, b, state) {
623
670
  }
624
671
 
625
672
  const { toString } = Object.prototype;
673
+ const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;
626
674
  /**
627
675
  * Create a comparator method based on the type-specific equality comparators passed.
628
676
  */
@@ -640,22 +688,30 @@ function createEqualityComparator(config) {
640
688
  * In all cases, these decisions should be reevaluated based on changes to the language and
641
689
  * common development practices.
642
690
  *
691
+ * When `Symbol.toStringTag` is a non-empty string (TypedArrays, `ArrayBuffer`, `DataView`, `Map`, `URL`,
692
+ * etc.), `toString()` returns `[object ${stringTag}]` regardless of the object's type, so the comparator
693
+ * is looked up by the `Symbol.toStringTag` value directly. This avoids building (and then hashing) the
694
+ * full tag string for every comparison. Otherwise the tag depends on the built-in type, and is captured
695
+ * with `toString()` as before.
696
+ *
643
697
  * Capturing the tag is reasonably performant in modern environments like v8 and SpiderMonkey. This is
644
698
  * kept out of the main comparator so that it stays within the engine's bytecode budget for inlining.
645
699
  * See [#207](https://github.com/planttheidea/fast-equals/pull/207) for more details.
646
700
  */
647
701
  const areTaggedObjectsEqual = getUnsupportedCustomComparator
648
702
  ? function areTaggedObjectsEqual(a, b, state) {
649
- const tag = toString.call(a);
703
+ const stringTag = toStringTag && a[toStringTag];
704
+ const tag = stringTag && typeof stringTag === 'string' ? stringTag : toString.call(a);
650
705
  const supportedComparator = supportedComparatorMap[tag];
651
706
  if (supportedComparator) {
652
707
  return supportedComparator(a, b, state);
653
708
  }
654
- const unsupportedCustomComparator = getUnsupportedCustomComparator(a, b, state, tag);
709
+ const unsupportedCustomComparator = getUnsupportedCustomComparator(a, b, state, tag === stringTag ? toString.call(a) : tag);
655
710
  return unsupportedCustomComparator ? unsupportedCustomComparator(a, b, state) : false;
656
711
  }
657
712
  : function areTaggedObjectsEqual(a, b, state) {
658
- const supportedComparator = supportedComparatorMap[toString.call(a)];
713
+ const stringTag = toStringTag && a[toStringTag];
714
+ const supportedComparator = supportedComparatorMap[stringTag && typeof stringTag === 'string' ? stringTag : toString.call(a)];
659
715
  return supportedComparator ? supportedComparator(a, b, state) : false;
660
716
  };
661
717
  /**
@@ -835,50 +891,61 @@ function createIsEqual({ circular, comparator, createState, equals, strict }) {
835
891
  };
836
892
  }
837
893
  /**
838
- * Create a map of `toString()` values to their respective handlers for `tag`-based lookups.
839
- */
840
- function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlSearchParamsEqual, areUrlsEqual, }) {
841
- return {
842
- '[object Arguments]': areObjectsEqual,
843
- '[object Array]': areArraysEqual,
844
- '[object ArrayBuffer]': areArrayBuffersEqual,
845
- '[object AsyncGeneratorFunction]': areFunctionsEqual,
846
- '[object BigInt]': arePrimitiveWrappersEqual,
847
- '[object BigInt64Array]': areTypedArraysEqual,
848
- '[object BigUint64Array]': areTypedArraysEqual,
849
- '[object Boolean]': arePrimitiveWrappersEqual,
850
- '[object DataView]': areDataViewsEqual,
851
- '[object Date]': areDatesEqual,
894
+ * Create a map of tags to their respective handlers for `tag`-based lookups. Each handler is keyed
895
+ * by both its `Symbol.toStringTag` value (`Map`) and its `toString()` value (`[object Map]`).
896
+ *
897
+ * @note
898
+ * The map has a `null` prototype, so a `Symbol.toStringTag` like `constructor` or `toString` cannot
899
+ * resolve to an inherited `Object.prototype` method.
900
+ */
901
+ function createSupportedComparatorMap(config) {
902
+ return [
903
+ ['Arguments', config.areObjectsEqual],
904
+ ['Array', config.areArraysEqual],
905
+ ['ArrayBuffer', config.areArrayBuffersEqual],
906
+ ['AsyncGeneratorFunction', config.areFunctionsEqual],
907
+ ['BigInt', config.arePrimitiveWrappersEqual],
908
+ ['BigInt64Array', config.areTypedArraysEqual],
909
+ ['BigUint64Array', config.areTypedArraysEqual],
910
+ ['Boolean', config.arePrimitiveWrappersEqual],
911
+ ['DataView', config.areDataViewsEqual],
912
+ ['Date', config.areDatesEqual],
852
913
  // If an error tag, it should be tested explicitly. Like RegExp, the properties are not
853
914
  // enumerable, and therefore will give false positives if tested like a standard object.
854
- '[object Error]': areErrorsEqual,
855
- '[object Float16Array]': areTypedArraysEqual,
856
- '[object Float32Array]': areTypedArraysEqual,
857
- '[object Float64Array]': areTypedArraysEqual,
858
- '[object Function]': areFunctionsEqual,
859
- '[object GeneratorFunction]': areFunctionsEqual,
860
- '[object Int8Array]': areTypedArraysEqual,
861
- '[object Int16Array]': areTypedArraysEqual,
862
- '[object Int32Array]': areTypedArraysEqual,
863
- '[object Map]': areMapsEqual,
864
- '[object Number]': arePrimitiveWrappersEqual,
865
- '[object Object]': (a, b, state) =>
866
- // The exception for value comparison is custom `Promise`-like class instances. These should
867
- // be treated the same as standard `Promise` objects, which means strict equality, and if
868
- // it reaches this point then that strict equality comparison has already failed.
869
- typeof a.then !== 'function' && typeof b.then !== 'function' && areObjectsEqual(a, b, state),
915
+ ['Error', config.areErrorsEqual],
916
+ ['Float16Array', config.areTypedArraysEqual],
917
+ ['Float32Array', config.areTypedArraysEqual],
918
+ ['Float64Array', config.areTypedArraysEqual],
919
+ ['Function', config.areFunctionsEqual],
920
+ ['GeneratorFunction', config.areFunctionsEqual],
921
+ ['Int8Array', config.areTypedArraysEqual],
922
+ ['Int16Array', config.areTypedArraysEqual],
923
+ ['Int32Array', config.areTypedArraysEqual],
924
+ ['Map', config.areMapsEqual],
925
+ ['Number', config.arePrimitiveWrappersEqual],
926
+ [
927
+ 'Object',
928
+ (a, b, state) =>
929
+ // The exception for value comparison is custom `Promise`-like class instances. These should
930
+ // be treated the same as standard `Promise` objects, which means strict equality, and if
931
+ // it reaches this point then that strict equality comparison has already failed.
932
+ typeof a.then !== 'function' && typeof b.then !== 'function' && config.areObjectsEqual(a, b, state),
933
+ ],
870
934
  // For RegExp, the properties are not enumerable, and therefore will give false positives if
871
935
  // tested like a standard object.
872
- '[object RegExp]': areRegExpsEqual,
873
- '[object Set]': areSetsEqual,
874
- '[object String]': arePrimitiveWrappersEqual,
875
- '[object URL]': areUrlsEqual,
876
- '[object URLSearchParams]': areUrlSearchParamsEqual,
877
- '[object Uint8Array]': areTypedArraysEqual,
878
- '[object Uint8ClampedArray]': areTypedArraysEqual,
879
- '[object Uint16Array]': areTypedArraysEqual,
880
- '[object Uint32Array]': areTypedArraysEqual,
881
- };
936
+ ['RegExp', config.areRegExpsEqual],
937
+ ['Set', config.areSetsEqual],
938
+ ['String', config.arePrimitiveWrappersEqual],
939
+ ['URL', config.areUrlsEqual],
940
+ ['URLSearchParams', config.areUrlSearchParamsEqual],
941
+ ['Uint8Array', config.areTypedArraysEqual],
942
+ ['Uint8ClampedArray', config.areTypedArraysEqual],
943
+ ['Uint16Array', config.areTypedArraysEqual],
944
+ ['Uint32Array', config.areTypedArraysEqual],
945
+ ].reduce((comparatorMap, [tag, comparator]) => {
946
+ comparatorMap[tag] = comparatorMap[`[object ${tag}]`] = comparator;
947
+ return comparatorMap;
948
+ }, Object.create(null));
882
949
  }
883
950
 
884
951
  /**
package/package.json CHANGED
@@ -10,20 +10,20 @@
10
10
  "description": "A blazing-fast equality comparison utility for a variety of use-cases",
11
11
  "devDependencies": {
12
12
  "@planttheidea/build-tools": "^2.0.8",
13
- "@types/lodash": "^4.17.25",
14
- "@types/node": "^24.19.1",
13
+ "@types/lodash": "^4.17.27",
14
+ "@types/node": "^24.19.2",
15
15
  "@types/ramda": "^0.32.0",
16
16
  "@types/react": "^19.3.0",
17
17
  "@types/react-dom": "^19.3.0",
18
- "@typescript-eslint/eslint-plugin": "^8.71.1",
19
- "@typescript-eslint/parser": "^8.71.1",
20
- "@vitest/coverage-v8": "^4.1.11",
18
+ "@typescript-eslint/eslint-plugin": "^8.73.0",
19
+ "@typescript-eslint/parser": "^8.73.0",
20
+ "@vitest/coverage-v8": "^5.1.1",
21
21
  "cli-table3": "^0.6.5",
22
22
  "decircularize": "^1.0.0",
23
23
  "deep-eql": "^5.0.2",
24
24
  "deep-equal": "^2.2.3",
25
25
  "dequal": "^2.0.3",
26
- "es-toolkit": "^1.52.0",
26
+ "es-toolkit": "^1.53.2",
27
27
  "eslint": "^9.39.5",
28
28
  "eslint-friendly-formatter": "^4.0.1",
29
29
  "eslint-plugin-import": "^2.32.0",
@@ -31,19 +31,19 @@
31
31
  "fast-glob": "^3.3.3",
32
32
  "lodash": "^4.18.1",
33
33
  "nano-equal": "^2.0.2",
34
- "prettier": "^3.9.9",
35
- "react": "^19.3.0",
36
- "react-dom": "^19.3.0",
37
- "react-fast-compare": "^3.2.2",
38
- "release-it": "^21.1.1",
39
- "rollup": "^4.64.3",
34
+ "prettier": "^3.10.1",
35
+ "react": "^19.3.1",
36
+ "react-dom": "^19.3.1",
37
+ "react-fast-compare": "^3.2.4",
38
+ "release-it": "^21.1.3",
39
+ "rollup": "^4.68.8",
40
40
  "shallow-equal-fuzzy": "^0.0.2",
41
- "tinybench": "^6.2.1",
41
+ "tinybench": "^6.2.4",
42
42
  "typescript": "^6.0.3",
43
- "typescript-eslint": "^8.71.1",
43
+ "typescript-eslint": "^8.73.0",
44
44
  "underscore": "^1.13.8",
45
- "vite": "^8.3.4",
46
- "vitest": "^4.1.11"
45
+ "vite": "^8.3.8",
46
+ "vitest": "^5.1.1"
47
47
  },
48
48
  "engines": {
49
49
  "node": ">=6.0.0"
@@ -79,6 +79,7 @@
79
79
  "main": "./dist/cjs/index.cjs",
80
80
  "module": "./dist/es/index.mjs",
81
81
  "name": "fast-equals",
82
+ "packageManager": "yarn@4.18.1",
82
83
  "publishConfig": {
83
84
  "access": "public"
84
85
  },
@@ -110,5 +111,5 @@
110
111
  "sideEffects": false,
111
112
  "type": "module",
112
113
  "types": "./index.d.ts",
113
- "version": "6.4.0"
114
+ "version": "6.4.2"
114
115
  }