Skip to content

Commit

Permalink
Core: Optimize equiv to use a predefined Set
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Aug 16, 2022
1 parent dac41dc commit 757fdac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/equiv.js
@@ -1,4 +1,7 @@
import { objectType, slice } from './core/utilities';
import { StringSet } from './globals';

const CONTAINER_TYPES = new StringSet(['object', 'array', 'map', 'set']);

// Value pairs queued for comparison. Used for breadth-first processing order, recursion
// detection and avoiding repeated comparison (see below for details).
Expand Down Expand Up @@ -55,17 +58,13 @@ function getRegExpFlags (regexp) {
return 'flags' in regexp ? regexp.flags : regexp.toString().match(/[gimuy]*$/)[0];
}

function isContainer (val) {
return ['object', 'array', 'map', 'set'].indexOf(objectType(val)) !== -1;
}

function breadthFirstCompareChild (a, b) {
// If a is a container not reference-equal to b, postpone the comparison to the
// end of the pairs queue -- unless (a, b) has been seen before, in which case skip
// over the pair.
if (a === b) {
return true;
} else if (!isContainer(a)) {
} else if (!CONTAINER_TYPES.has(objectType(a))) {
return typeEquiv(a, b);
} else if (pairs.every((pair) => pair.a !== a || pair.b !== b)) {
// Not yet started comparing this pair
Expand Down
12 changes: 12 additions & 0 deletions src/globals.js
Expand Up @@ -112,3 +112,15 @@ export const StringMap = typeof g.Map === 'function' &&
});
}
};

export const StringSet = window.Set || function () {
const set = Object.create(null);
return {
add (value) {
set[value] = true;
},
has (value) {
return value in set;
}
};
};

0 comments on commit 757fdac

Please sign in to comment.