From 71a77c7fcfc2e1a512925261fd6f762d67f50325 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Wed, 6 Sep 2023 11:12:15 +0200 Subject: [PATCH] Use a map for Onyx collection keys --- lib/Onyx.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index 27f49aec..c8ffcf82 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -27,8 +27,8 @@ let lastConnectionID = 0; // Holds a mapping of all the react components that want their state subscribed to a store key const callbackToStateMapping = {}; -// Stores all of the keys that Onyx can use. Must be defined in init(). -let onyxKeys = {}; +// Keeps a copy of the values of the onyx collection keys as a map for faster lookups +let onyxCollectionKeyMap = new Map(); // Holds a list of keys that have been directly subscribed to or recently modified from least to most recent let recentlyAccessedKeys = []; @@ -141,7 +141,7 @@ function getAllKeys() { * @returns {Boolean} */ function isCollectionKey(key) { - return _.contains(_.values(onyxKeys.COLLECTION), key); + return onyxCollectionKeyMap.has(key); } /** @@ -1392,8 +1392,13 @@ function init({ cache.setRecentKeysLimit(maxCachedKeysCount); } - // Let Onyx know about all of our keys - onyxKeys = keys; + // We need the value of the collection keys later for checking if a + // key is a collection. We store it in a map for faster lookup. + const collectionValues = _.values(keys.COLLECTION); + onyxCollectionKeyMap = _.reduce(collectionValues, (acc, val) => { + acc.set(val, true); + return acc; + }, new Map()); // Set our default key states to use when initializing and clearing Onyx data defaultKeyStates = initialKeyStates;