From 190f533cd460e520764e382ea091859d83391da5 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 15 May 2024 17:49:39 +0500 Subject: [PATCH 1/3] perf: use forEach method from Set instead of converting first to Array and then filtering it --- lib/Onyx.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Onyx.ts b/lib/Onyx.ts index 1474e33d..6a0521a0 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -120,8 +120,13 @@ function connect(connectOptions: ConnectOptions): nu // We search all the keys in storage to see if any are a "match" for the subscriber we are connecting so that we // can send data back to the subscriber. Note that multiple keys can match as a subscriber could either be // subscribed to a "collection key" or a single key. - const matchingKeys = Array.from(keys).filter((key) => OnyxUtils.isKeyMatch(mapping.key, key)); - + const matchingKeys: string[] = []; + keys.forEach((key) => { + if (!OnyxUtils.isKeyMatch(mapping.key, key)) { + return; + } + matchingKeys.push(key); + }); // If the key being connected to does not exist we initialize the value with null. For subscribers that connected // directly via connect() they will simply get a null value sent to them without any information about which key matched // since there are none matched. In withOnyx() we wait for all connected keys to return a value before rendering the child From 12f3c5d26139fbd104955be8ff81891dd7d86ed5 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 15 May 2024 17:49:46 +0500 Subject: [PATCH 2/3] perf: use forEach method from Set instead of converting first to Array, then filtering it and then reducing that array --- lib/OnyxUtils.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index e0649685..d00fb805 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -311,17 +311,14 @@ function tryGetCachedValue(key: TKey, mapping?: Partial k.startsWith(key)); - const values = matchingKeys.reduce((finalObject: NonNullable>, matchedKey) => { - const cachedValue = cache.getValue(matchedKey); - if (cachedValue) { - // This is permissible because we're in the process of constructing the final object in a reduce function. - // eslint-disable-next-line no-param-reassign - finalObject[matchedKey] = cachedValue; + const values: Record = {}; + allCacheKeys.forEach((cacheKey) => { + if (!cacheKey.startsWith(key)) { + return; } - return finalObject; - }, {}); + values[cacheKey] = cache.getValue(cacheKey); + }); val = values; } From e5388e7cce5c7eda4c35f1266a69ede6e9a602dc Mon Sep 17 00:00:00 2001 From: Tomasz Lesniakiewicz Date: Mon, 20 May 2024 13:16:29 +0200 Subject: [PATCH 3/3] refoctor: adjust types --- lib/OnyxUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index d00fb805..ba3b12b6 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -311,7 +311,7 @@ function tryGetCachedValue(key: TKey, mapping?: Partial = {}; + const values: OnyxCollection = {}; allCacheKeys.forEach((cacheKey) => { if (!cacheKey.startsWith(key)) { return;