Skip to content

Commit

Permalink
leverage isView instead of tag check
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Feb 26, 2023
1 parent c760179 commit c2815f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 103 deletions.
64 changes: 0 additions & 64 deletions __tests__/__helpers__/testSuites.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,6 @@ export default [
value1: new Float32Array([21, 31]),
value2: new Float32Array([31, 21]),
},
{
deepEqual: false,
description: 'Float32Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Float32Array([21, 31]),
value2: new Float64Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Float64Array objects',
Expand All @@ -646,13 +639,6 @@ export default [
value1: new Float64Array([21, 31]),
value2: new Float64Array([31, 21]),
},
{
deepEqual: false,
description: 'Float64Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Float64Array([21, 31]),
value2: new Float32Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Int8Array objects',
Expand All @@ -667,13 +653,6 @@ export default [
value1: new Int8Array([21, 31]),
value2: new Int8Array([31, 21]),
},
{
deepEqual: false,
description: 'Int8Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Int8Array([21, 31]),
value2: new Int16Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Int16Array objects',
Expand All @@ -688,13 +667,6 @@ export default [
value1: new Int16Array([21, 31]),
value2: new Int16Array([31, 21]),
},
{
deepEqual: false,
description: 'Int16Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Int16Array([21, 31]),
value2: new Int32Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Int32Array objects',
Expand All @@ -709,13 +681,6 @@ export default [
value1: new Int32Array([21, 31]),
value2: new Int32Array([31, 21]),
},
{
deepEqual: false,
description: 'Int32Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Int32Array([21, 31]),
value2: new Int8Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Uint8Array objects',
Expand All @@ -730,13 +695,6 @@ export default [
value1: new Uint8Array([21, 31]),
value2: new Uint8Array([31, 21]),
},
{
deepEqual: false,
description: 'Uint8Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Uint8Array([21, 31]),
value2: new Uint8ClampedArray([21, 31]),
},
{
deepEqual: true,
description: 'equal Uint8ClampedArray objects',
Expand All @@ -751,14 +709,6 @@ export default [
value1: new Uint8ClampedArray([21, 31]),
value2: new Uint8ClampedArray([31, 21]),
},
{
deepEqual: false,
description:
'Uint8ClampedArray and a different TypedArray are not equal',
shallowEqual: false,
value1: new Uint8ClampedArray([21, 31]),
value2: new Uint16Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Uint16Array objects',
Expand All @@ -773,13 +723,6 @@ export default [
value1: new Uint16Array([21, 31]),
value2: new Uint16Array([31, 21]),
},
{
deepEqual: false,
description: 'Uint16Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Uint16Array([21, 31]),
value2: new Uint32Array([21, 31]),
},
{
deepEqual: true,
description: 'equal Uint32Array objects',
Expand All @@ -794,13 +737,6 @@ export default [
value1: new Uint32Array([21, 31]),
value2: new Uint32Array([31, 21]),
},
{
deepEqual: false,
description: 'Uint32Array and a different TypedArray are not equal',
shallowEqual: false,
value1: new Uint32Array([21, 31]),
value2: new Uint8Array([21, 31]),
},
],
},
{
Expand Down
50 changes: 11 additions & 39 deletions src/comparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
areSetsEqual as areSetsEqualDefault,
areTypedArraysEqual,
} from './equals';
import { combineComparators, createIsCircular } from './utils';
import { combineComparators, createIsCircular, isArrayBuffer } from './utils';
import type {
ComparatorConfig,
CustomEqualCreatorOptions,
Expand All @@ -20,43 +20,12 @@ import type {
const ARGUMENTS_TAG = '[object Arguments]';
const BOOLEAN_TAG = '[object Boolean]';
const DATE_TAG = '[object Date]';
const BIG_INT_64_ARRAY_TAG = '[object BigInt64Array]';
const BIG_UINT_64_ARRAY_TAG = '[object BigUint64Array]';
const FLOAT_32_ARRAY_TAG = '[object Float32Array]';
const FLOAT_64_ARRAY_TAG = '[object Float64Array]';
const INT_8_ARRAY_TAG = '[object Int8Array]';
const INT_16_ARRAY_TAG = '[object Int16Array]';
const INT_32_ARRAY_TAG = '[object Int32Array]';
const MAP_TAG = '[object Map]';
const NUMBER_TAG = '[object Number]';
const OBJECT_TAG = '[object Object]';
const REG_EXP_TAG = '[object RegExp]';
const SET_TAG = '[object Set]';
const STRING_TAG = '[object String]';
const UINT_8_ARRAY_TAG = '[object Uint8Array]';
const UINT_8_CLAMPED_ARRAY_TAG = '[object Uint8ClampedArray]';
const UINT_16_ARRAY_TAG = '[object Uint16Array]';
const UINT_32_ARRAY_TAG = '[object Uint32Array]';

const PRIMITIVE_WRAPPER: Record<string, true | undefined> = {
[BOOLEAN_TAG]: true,
[NUMBER_TAG]: true,
[STRING_TAG]: true,
};

const TYPED_ARRAY: Record<string, true | undefined> = {
[BIG_INT_64_ARRAY_TAG]: true,
[BIG_UINT_64_ARRAY_TAG]: true,
[FLOAT_32_ARRAY_TAG]: true,
[FLOAT_64_ARRAY_TAG]: true,
[INT_8_ARRAY_TAG]: true,
[INT_16_ARRAY_TAG]: true,
[INT_32_ARRAY_TAG]: true,
[UINT_8_ARRAY_TAG]: true,
[UINT_8_CLAMPED_ARRAY_TAG]: true,
[UINT_16_ARRAY_TAG]: true,
[UINT_32_ARRAY_TAG]: true,
};

const { isArray } = Array;
const { assign } = Object;
Expand Down Expand Up @@ -113,13 +82,20 @@ export function createComparator<Meta>({
// `isArray()` works on subclasses and is cross-realm, so we can again avoid
// the `toString.call()` cost unless necessary by just checking if either
// and then both are arrays.
const aArray = isArray(a);
const bArray = isArray(b);
let aArray = isArray(a);
let bArray = isArray(b);

if (aArray || bArray) {
return aArray === bArray && areArraysEqual(a, b, state);
}

aArray = isArrayBuffer(a);
bArray = isArrayBuffer(b);

if (aArray || bArray) {
return aArray === bArray && areTypedArraysEqual(a, b, state);
}

// Since this is a custom object, use the classic `toString.call()` to get its
// type. This is reasonably performant in modern environments like v8 and
// SpiderMonkey, and allows for cross-realm comparison when other checks like
Expand Down Expand Up @@ -164,14 +140,10 @@ export function createComparator<Meta>({
return areObjectsEqual(a, b, state);
}

if (TYPED_ARRAY[tag]) {
return areTypedArraysEqual(a, b, state);
}

// As the penultimate fallback, check if the values passed are primitive wrappers. This
// is very rare in modern JS, which is why it is deprioritized compared to all other object
// types.
if (PRIMITIVE_WRAPPER[tag]) {
if (tag === BOOLEAN_TAG || tag === NUMBER_TAG || tag === STRING_TAG) {
return arePrimitiveWrappersEqual(a, b, state);
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
const { hasOwnProperty } = Object.prototype;

const SUPPORTS_ARRAY_BUFFER =
typeof ArrayBuffer === 'function' && Boolean(ArrayBuffer.isView);

/**
* Combine two comparators into a single comparators.
*/
Expand Down Expand Up @@ -102,6 +105,10 @@ export const hasOwn =
((object: Dictionary, property: number | string | symbol) =>
hasOwnProperty.call(object, property));

export function isArrayBuffer(value: any): value is ArrayBuffer {
return SUPPORTS_ARRAY_BUFFER && ArrayBuffer.isView(value);
}

/**
* Whether the values passed are strictly equal or both NaN.
*/
Expand Down

0 comments on commit c2815f1

Please sign in to comment.