From 757fdac3be60dce0a46b4a456b09efbbb24d69b1 Mon Sep 17 00:00:00 2001 From: Izel Nakri Date: Wed, 17 Aug 2022 00:18:55 +0200 Subject: [PATCH] Core: Optimize equiv to use a predefined Set --- src/equiv.js | 9 ++++----- src/globals.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/equiv.js b/src/equiv.js index 4060310ac..779e36f7b 100644 --- a/src/equiv.js +++ b/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). @@ -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 diff --git a/src/globals.js b/src/globals.js index ac7e3dab3..98b20a551 100644 --- a/src/globals.js +++ b/src/globals.js @@ -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; + } + }; +};