fast-equals 6.0.2 → 6.0.3

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 ~2kB when minified and gzipped.
4
+ various use-cases. It has no dependencies, and is ~2.3kB when minified and gzipped.
5
5
 
6
6
  The following types are handled out-of-the-box:
7
7
 
@@ -12,7 +12,9 @@ The following types are handled out-of-the-box:
12
12
  - `RegExp` objects
13
13
  - `Map` / `Set` iterables
14
14
  - `Promise` objects and then-ables
15
- - Primitive wrappers (`new Boolean()` / `new Number()` / `new String()`)
15
+ - `Error` objects, including subclasses and their own properties
16
+ - `URL` objects
17
+ - Primitive wrappers (`new Boolean()` / `new Number()` / `new String()`, and boxed `BigInt`)
16
18
  - Custom class instances, including subclasses of native classes
17
19
 
18
20
  Methods are available for deep, shallow, [`SameValue`](http://ecma-international.org/ecma-262/7.0/#sec-samevalue),
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
4
- // eslint-disable-next-line @typescript-eslint/unbound-method
5
4
  const { hasOwnProperty } = Object.prototype;
6
5
  /**
7
6
  * Combine two comparators into a single comparators.
@@ -55,6 +54,9 @@ Object.hasOwn || ((object, property) => hasOwnProperty.call(object, property));
55
54
  const PREACT_VNODE = '__v';
56
55
  const PREACT_OWNER = '__o';
57
56
  const REACT_OWNER = '_owner';
57
+ // `Float16Array` is recent enough that it cannot be referenced unguarded, and capturing its
58
+ // availability once keeps that detail out of the comparison itself.
59
+ const HAS_FLOAT_16_ARRAY = typeof Float16Array !== 'undefined';
58
60
  const { getOwnPropertyDescriptor, keys } = Object;
59
61
  /**
60
62
  * Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
@@ -126,9 +128,19 @@ function areDatesEqual(a, b) {
126
128
  }
127
129
  /**
128
130
  * Whether the errors passed are equal in value.
129
- */
130
- function areErrorsEqual(a, b) {
131
- return a.name === b.name && a.message === b.message && a.cause === b.cause && a.stack === b.stack;
131
+ *
132
+ * @note
133
+ * `name`, `message` and `stack` are own properties but are not enumerable, so they are compared
134
+ * explicitly. `cause` is compared by value rather than by reference, matching how every other
135
+ * nested value in the comparison is treated. Own enumerable properties (which custom `Error`
136
+ * subclasses commonly add) are compared by composing this with the object comparator in the
137
+ * comparator config, so that the strict and circular variants apply to them as well.
138
+ */
139
+ function areErrorsEqual(a, b, state) {
140
+ return (a.name === b.name
141
+ && a.message === b.message
142
+ && a.stack === b.stack
143
+ && state.equals(a.cause, b.cause, 'cause', 'cause', a, b, state));
132
144
  }
133
145
  /**
134
146
  * Whether the `Map`s are equal in value.
@@ -294,6 +306,19 @@ function areTypedArraysEqual(a, b) {
294
306
  if (b.length !== index || a.byteOffset !== b.byteOffset) {
295
307
  return false;
296
308
  }
309
+ // Only float-backed views can hold `NaN`, and the additional check needed to treat it as equal
310
+ // to itself measurably slows the loop, so integer views keep the plain comparison. This is
311
+ // hoisted out of the loop so the cost is paid once per call rather than once per element.
312
+ if (a instanceof Float64Array || a instanceof Float32Array || (HAS_FLOAT_16_ARRAY && a instanceof Float16Array)) {
313
+ while (index-- > 0) {
314
+ // `NaN` is the only value not equal to itself, and it is treated as equal here to match
315
+ // the SameValueZero semantics used for every other numeric comparison in the library.
316
+ if (a[index] !== b[index] && (a[index] === a[index] || b[index] === b[index])) {
317
+ return false;
318
+ }
319
+ }
320
+ return true;
321
+ }
297
322
  while (index-- > 0) {
298
323
  if (a[index] !== b[index]) {
299
324
  return false;
@@ -305,13 +330,45 @@ function areTypedArraysEqual(a, b) {
305
330
  * Whether the URL instances are equal in value.
306
331
  */
307
332
  function areUrlsEqual(a, b) {
308
- return (a.hostname === b.hostname
333
+ // `href` is the normalized serialization of every component, so matching hrefs are equal without
334
+ // any further work. Only a difference in query parameter ordering can survive a mismatch here.
335
+ if (a.href === b.href) {
336
+ return true;
337
+ }
338
+ return (a.protocol === b.protocol
339
+ && a.username === b.username
340
+ && a.password === b.password
341
+ // `host` covers both the hostname and the port.
342
+ && a.host === b.host
309
343
  && a.pathname === b.pathname
310
- && a.protocol === b.protocol
311
- && a.port === b.port
312
344
  && a.hash === b.hash
313
- && a.username === b.username
314
- && a.password === b.password);
345
+ && areSearchParamsEqual(a.searchParams, b.searchParams));
346
+ }
347
+ /**
348
+ * Whether the search params passed are equal in value.
349
+ *
350
+ * @note
351
+ * Order is not significant, matching how the other unordered collections in the library are
352
+ * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
353
+ * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
354
+ */
355
+ function areSearchParamsEqual(a, b) {
356
+ const serializedA = a.toString();
357
+ const serializedB = b.toString();
358
+ // Identical serializations are equal under any ordering, and this is by far the common case, so
359
+ // it is worth checking before sorting anything.
360
+ return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
361
+ }
362
+ /**
363
+ * Reorder a serialized query string so that params holding the same pairs compare as equal
364
+ * regardless of the order they appear in.
365
+ *
366
+ * @note
367
+ * The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
368
+ * splitting on `&` recovers exactly the pairs and nothing else.
369
+ */
370
+ function sortSearchParams(serialized) {
371
+ return serialized.split('&').sort().join('&');
315
372
  }
316
373
  function isPropertyEqual(a, b, state, property) {
317
374
  if ((property === REACT_OWNER || property === PREACT_OWNER || property === PREACT_VNODE)
@@ -321,7 +378,6 @@ function isPropertyEqual(a, b, state, property) {
321
378
  return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
322
379
  }
323
380
 
324
- // eslint-disable-next-line @typescript-eslint/unbound-method
325
381
  const toString = Object.prototype.toString;
326
382
  /**
327
383
  * Create a comparator method based on the type-specific equality comparators passed.
@@ -434,7 +490,13 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
434
490
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
435
491
  areDataViewsEqual,
436
492
  areDatesEqual: areDatesEqual,
437
- areErrorsEqual: areErrorsEqual,
493
+ // `Error` subclasses routinely carry their own enumerable properties (`status`, `code`, ...),
494
+ // which the error comparator alone does not see, so it is composed with the object comparator.
495
+ // `name` / `message` / `stack` are own but not enumerable, which is why errors need a
496
+ // comparator of their own rather than being treated as plain objects in the first place.
497
+ areErrorsEqual: strict
498
+ ? combineComparators(areErrorsEqual, areObjectsEqualStrict)
499
+ : combineComparators(areErrorsEqual, areObjectsEqual),
438
500
  areFunctionsEqual: strictEqual,
439
501
  areMapsEqual: strict ? combineComparators(areMapsEqual, areObjectsEqualStrict) : areMapsEqual,
440
502
  areNumbersEqual: sameValueEqual,
@@ -453,11 +515,15 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
453
515
  }
454
516
  if (circular) {
455
517
  const areArraysEqual = createIsCircular(config.areArraysEqual);
518
+ // Errors are included because comparing `cause` and own properties by value means an error
519
+ // that references itself would otherwise recurse without bound.
520
+ const areErrorsEqual = createIsCircular(config.areErrorsEqual);
456
521
  const areMapsEqual = createIsCircular(config.areMapsEqual);
457
522
  const areObjectsEqual = createIsCircular(config.areObjectsEqual);
458
523
  const areSetsEqual = createIsCircular(config.areSetsEqual);
459
524
  config = Object.assign({}, config, {
460
525
  areArraysEqual,
526
+ areErrorsEqual,
461
527
  areMapsEqual,
462
528
  areObjectsEqual,
463
529
  areSetsEqual,
@@ -512,13 +578,13 @@ function createIsEqual({ circular, comparator, createState, equals, strict }) {
512
578
  /**
513
579
  * Create a map of `toString()` values to their respective handlers for `tag`-based lookups.
514
580
  */
515
- function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlsEqual, }) {
581
+ function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlsEqual, }) {
516
582
  return {
517
583
  '[object Arguments]': areObjectsEqual,
518
584
  '[object Array]': areArraysEqual,
519
585
  '[object ArrayBuffer]': areArrayBuffersEqual,
520
586
  '[object AsyncGeneratorFunction]': areFunctionsEqual,
521
- '[object BigInt]': areNumbersEqual,
587
+ '[object BigInt]': arePrimitiveWrappersEqual,
522
588
  '[object BigInt64Array]': areTypedArraysEqual,
523
589
  '[object BigUint64Array]': areTypedArraysEqual,
524
590
  '[object Boolean]': arePrimitiveWrappersEqual,
package/dist/es/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
2
- // eslint-disable-next-line @typescript-eslint/unbound-method
3
2
  const { hasOwnProperty } = Object.prototype;
4
3
  /**
5
4
  * Combine two comparators into a single comparators.
@@ -53,6 +52,9 @@ Object.hasOwn || ((object, property) => hasOwnProperty.call(object, property));
53
52
  const PREACT_VNODE = '__v';
54
53
  const PREACT_OWNER = '__o';
55
54
  const REACT_OWNER = '_owner';
55
+ // `Float16Array` is recent enough that it cannot be referenced unguarded, and capturing its
56
+ // availability once keeps that detail out of the comparison itself.
57
+ const HAS_FLOAT_16_ARRAY = typeof Float16Array !== 'undefined';
56
58
  const { getOwnPropertyDescriptor, keys } = Object;
57
59
  /**
58
60
  * Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
@@ -124,9 +126,19 @@ function areDatesEqual(a, b) {
124
126
  }
125
127
  /**
126
128
  * Whether the errors passed are equal in value.
127
- */
128
- function areErrorsEqual(a, b) {
129
- return a.name === b.name && a.message === b.message && a.cause === b.cause && a.stack === b.stack;
129
+ *
130
+ * @note
131
+ * `name`, `message` and `stack` are own properties but are not enumerable, so they are compared
132
+ * explicitly. `cause` is compared by value rather than by reference, matching how every other
133
+ * nested value in the comparison is treated. Own enumerable properties (which custom `Error`
134
+ * subclasses commonly add) are compared by composing this with the object comparator in the
135
+ * comparator config, so that the strict and circular variants apply to them as well.
136
+ */
137
+ function areErrorsEqual(a, b, state) {
138
+ return (a.name === b.name
139
+ && a.message === b.message
140
+ && a.stack === b.stack
141
+ && state.equals(a.cause, b.cause, 'cause', 'cause', a, b, state));
130
142
  }
131
143
  /**
132
144
  * Whether the `Map`s are equal in value.
@@ -292,6 +304,19 @@ function areTypedArraysEqual(a, b) {
292
304
  if (b.length !== index || a.byteOffset !== b.byteOffset) {
293
305
  return false;
294
306
  }
307
+ // Only float-backed views can hold `NaN`, and the additional check needed to treat it as equal
308
+ // to itself measurably slows the loop, so integer views keep the plain comparison. This is
309
+ // hoisted out of the loop so the cost is paid once per call rather than once per element.
310
+ if (a instanceof Float64Array || a instanceof Float32Array || (HAS_FLOAT_16_ARRAY && a instanceof Float16Array)) {
311
+ while (index-- > 0) {
312
+ // `NaN` is the only value not equal to itself, and it is treated as equal here to match
313
+ // the SameValueZero semantics used for every other numeric comparison in the library.
314
+ if (a[index] !== b[index] && (a[index] === a[index] || b[index] === b[index])) {
315
+ return false;
316
+ }
317
+ }
318
+ return true;
319
+ }
295
320
  while (index-- > 0) {
296
321
  if (a[index] !== b[index]) {
297
322
  return false;
@@ -303,13 +328,45 @@ function areTypedArraysEqual(a, b) {
303
328
  * Whether the URL instances are equal in value.
304
329
  */
305
330
  function areUrlsEqual(a, b) {
306
- return (a.hostname === b.hostname
331
+ // `href` is the normalized serialization of every component, so matching hrefs are equal without
332
+ // any further work. Only a difference in query parameter ordering can survive a mismatch here.
333
+ if (a.href === b.href) {
334
+ return true;
335
+ }
336
+ return (a.protocol === b.protocol
337
+ && a.username === b.username
338
+ && a.password === b.password
339
+ // `host` covers both the hostname and the port.
340
+ && a.host === b.host
307
341
  && a.pathname === b.pathname
308
- && a.protocol === b.protocol
309
- && a.port === b.port
310
342
  && a.hash === b.hash
311
- && a.username === b.username
312
- && a.password === b.password);
343
+ && areSearchParamsEqual(a.searchParams, b.searchParams));
344
+ }
345
+ /**
346
+ * Whether the search params passed are equal in value.
347
+ *
348
+ * @note
349
+ * Order is not significant, matching how the other unordered collections in the library are
350
+ * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
351
+ * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
352
+ */
353
+ function areSearchParamsEqual(a, b) {
354
+ const serializedA = a.toString();
355
+ const serializedB = b.toString();
356
+ // Identical serializations are equal under any ordering, and this is by far the common case, so
357
+ // it is worth checking before sorting anything.
358
+ return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
359
+ }
360
+ /**
361
+ * Reorder a serialized query string so that params holding the same pairs compare as equal
362
+ * regardless of the order they appear in.
363
+ *
364
+ * @note
365
+ * The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
366
+ * splitting on `&` recovers exactly the pairs and nothing else.
367
+ */
368
+ function sortSearchParams(serialized) {
369
+ return serialized.split('&').sort().join('&');
313
370
  }
314
371
  function isPropertyEqual(a, b, state, property) {
315
372
  if ((property === REACT_OWNER || property === PREACT_OWNER || property === PREACT_VNODE)
@@ -319,7 +376,6 @@ function isPropertyEqual(a, b, state, property) {
319
376
  return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
320
377
  }
321
378
 
322
- // eslint-disable-next-line @typescript-eslint/unbound-method
323
379
  const toString = Object.prototype.toString;
324
380
  /**
325
381
  * Create a comparator method based on the type-specific equality comparators passed.
@@ -432,7 +488,13 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
432
488
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
433
489
  areDataViewsEqual,
434
490
  areDatesEqual: areDatesEqual,
435
- areErrorsEqual: areErrorsEqual,
491
+ // `Error` subclasses routinely carry their own enumerable properties (`status`, `code`, ...),
492
+ // which the error comparator alone does not see, so it is composed with the object comparator.
493
+ // `name` / `message` / `stack` are own but not enumerable, which is why errors need a
494
+ // comparator of their own rather than being treated as plain objects in the first place.
495
+ areErrorsEqual: strict
496
+ ? combineComparators(areErrorsEqual, areObjectsEqualStrict)
497
+ : combineComparators(areErrorsEqual, areObjectsEqual),
436
498
  areFunctionsEqual: strictEqual,
437
499
  areMapsEqual: strict ? combineComparators(areMapsEqual, areObjectsEqualStrict) : areMapsEqual,
438
500
  areNumbersEqual: sameValueEqual,
@@ -451,11 +513,15 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
451
513
  }
452
514
  if (circular) {
453
515
  const areArraysEqual = createIsCircular(config.areArraysEqual);
516
+ // Errors are included because comparing `cause` and own properties by value means an error
517
+ // that references itself would otherwise recurse without bound.
518
+ const areErrorsEqual = createIsCircular(config.areErrorsEqual);
454
519
  const areMapsEqual = createIsCircular(config.areMapsEqual);
455
520
  const areObjectsEqual = createIsCircular(config.areObjectsEqual);
456
521
  const areSetsEqual = createIsCircular(config.areSetsEqual);
457
522
  config = Object.assign({}, config, {
458
523
  areArraysEqual,
524
+ areErrorsEqual,
459
525
  areMapsEqual,
460
526
  areObjectsEqual,
461
527
  areSetsEqual,
@@ -510,13 +576,13 @@ function createIsEqual({ circular, comparator, createState, equals, strict }) {
510
576
  /**
511
577
  * Create a map of `toString()` values to their respective handlers for `tag`-based lookups.
512
578
  */
513
- function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlsEqual, }) {
579
+ function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlsEqual, }) {
514
580
  return {
515
581
  '[object Arguments]': areObjectsEqual,
516
582
  '[object Array]': areArraysEqual,
517
583
  '[object ArrayBuffer]': areArrayBuffersEqual,
518
584
  '[object AsyncGeneratorFunction]': areFunctionsEqual,
519
- '[object BigInt]': areNumbersEqual,
585
+ '[object BigInt]': arePrimitiveWrappersEqual,
520
586
  '[object BigInt64Array]': areTypedArraysEqual,
521
587
  '[object BigUint64Array]': areTypedArraysEqual,
522
588
  '[object Boolean]': arePrimitiveWrappersEqual,
package/package.json CHANGED
@@ -9,41 +9,41 @@
9
9
  },
10
10
  "description": "A blazing-fast equality comparison utility for a variety of use-cases",
11
11
  "devDependencies": {
12
- "@planttheidea/build-tools": "^2.0.2",
13
- "@types/lodash": "^4.17.24",
12
+ "@planttheidea/build-tools": "^2.0.8",
13
+ "@types/lodash": "^4.17.25",
14
14
  "@types/node": "^24.13.3",
15
15
  "@types/ramda": "^0.32.0",
16
- "@types/react": "^19.2.17",
17
- "@types/react-dom": "^19.2.3",
18
- "@typescript-eslint/eslint-plugin": "^8.63.0",
19
- "@typescript-eslint/parser": "^8.63.0",
20
- "@vitest/coverage-v8": "^4.1.10",
16
+ "@types/react": "^19.2.18",
17
+ "@types/react-dom": "^19.2.5",
18
+ "@typescript-eslint/eslint-plugin": "^8.69.0",
19
+ "@typescript-eslint/parser": "^8.69.0",
20
+ "@vitest/coverage-v8": "^4.1.11",
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.49.0",
27
- "eslint": "^9.39.4",
26
+ "es-toolkit": "^1.52.0",
27
+ "eslint": "^9.39.5",
28
28
  "eslint-friendly-formatter": "^4.0.1",
29
29
  "eslint-plugin-import": "^2.32.0",
30
30
  "fast-deep-equal": "^3.1.3",
31
31
  "fast-glob": "^3.3.3",
32
32
  "lodash": "^4.18.1",
33
33
  "nano-equal": "^2.0.2",
34
- "prettier": "^3.9.4",
35
- "react": "^19.2.7",
36
- "react-dom": "^19.2.7",
34
+ "prettier": "^3.9.6",
35
+ "react": "^19.2.8",
36
+ "react-dom": "^19.2.8",
37
37
  "react-fast-compare": "^3.2.2",
38
- "release-it": "^20.2.1",
39
- "rollup": "^4.62.2",
38
+ "release-it": "^21.0.2",
39
+ "rollup": "^4.63.1",
40
40
  "shallow-equal-fuzzy": "^0.0.2",
41
- "tinybench": "^6.0.2",
42
- "typescript": "^5.9.3",
43
- "typescript-eslint": "^8.63.0",
41
+ "tinybench": "^6.1.4",
42
+ "typescript": "^6.0.3",
43
+ "typescript-eslint": "^8.69.0",
44
44
  "underscore": "^1.13.8",
45
- "vite": "^8.1.3",
46
- "vitest": "^4.1.10"
45
+ "vite": "^8.2.2",
46
+ "vitest": "^4.1.11"
47
47
  },
48
48
  "engines": {
49
49
  "node": ">=6.0.0"
@@ -110,5 +110,5 @@
110
110
  "sideEffects": false,
111
111
  "type": "module",
112
112
  "types": "./index.d.ts",
113
- "version": "6.0.2"
113
+ "version": "6.0.3"
114
114
  }