From 52b35cdb893ed9d4eacadc334cda846a51d41cd7 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Fri, 2 Aug 2024 17:41:26 +0500 Subject: [PATCH 1/3] perf: use startsWith instead of Str.startsWith as it adds additional overhead on huge accounts --- lib/OnyxUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index 8e6a6076..586d4068 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -384,7 +384,7 @@ function isCollectionKey(key: OnyxKey): key is CollectionKeyBase { } function isCollectionMemberKey(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}` { - return Str.startsWith(key, collectionKey) && key.length > collectionKey.length; + return key.startsWith(collectionKey) && key.length > collectionKey.length; } /** From c5c8174f766da313b0ab4cf441291d9d9df25936 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 5 Aug 2024 19:19:00 +0500 Subject: [PATCH 2/3] perf: avoid calculating collection key length and pass it as a const --- lib/Onyx.ts | 3 ++- lib/OnyxUtils.ts | 11 +++++++---- lib/useOnyx.ts | 6 +++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Onyx.ts b/lib/Onyx.ts index f927f353..3a591757 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -645,6 +645,7 @@ function updateSnapshots(data: OnyxUpdate[]) { const promises: Array<() => Promise> = []; const snapshotCollection = OnyxUtils.getCachedCollection(snapshotCollectionKey); + const snapshotCollectionKeyLength = snapshotCollectionKey.length; Object.entries(snapshotCollection).forEach(([snapshotKey, snapshotValue]) => { // Snapshots may not be present in cache. We don't know how to update them so we skip. @@ -656,7 +657,7 @@ function updateSnapshots(data: OnyxUpdate[]) { data.forEach(({key, value}) => { // snapshots are normal keys so we want to skip update if they are written to Onyx - if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key)) { + if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key, snapshotCollectionKeyLength)) { return; } diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index 1aa45eb8..5f22b964 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -394,8 +394,8 @@ function isCollectionKey(key: OnyxKey): key is CollectionKeyBase { return onyxCollectionKeySet.has(key); } -function isCollectionMemberKey(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}` { - return key.startsWith(collectionKey) && key.length > collectionKey.length; +function isCollectionMemberKey(collectionKey: TCollectionKey, key: string, collectionKeyLength: number): key is `${TCollectionKey}${string}` { + return Str.startsWith(key, collectionKey) && key.length > collectionKeyLength; } /** @@ -554,12 +554,14 @@ function getCachedCollection(collectionKey: TKey const allKeys = collectionMemberKeys || cache.getAllKeys(); const collection: OnyxCollection = {}; + const collectionKeyLength = collectionKey.length; + // forEach exists on both Set and Array allKeys.forEach((key) => { // If we don't have collectionMemberKeys array then we have to check whether a key is a collection member key. // Because in that case the keys will be coming from `cache.getAllKeys()` and we need to filter out the keys that // are not part of the collection. - if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key)) { + if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key, collectionKeyLength)) { return; } @@ -595,6 +597,7 @@ function keysChanged( // individual collection key member for the collection that is being updated. It is important to note that the collection parameter cane be a PARTIAL collection // and does not represent all of the combined keys and values for a collection key. It is just the "new" data that was merged in via mergeCollection(). const stateMappingKeys = Object.keys(callbackToStateMapping); + const collectionKeyLength = collectionKey.length; for (let i = 0; i < stateMappingKeys.length; i++) { const subscriber = callbackToStateMapping[stateMappingKeys[i]]; if (!subscriber) { @@ -614,7 +617,7 @@ function keysChanged( /** * e.g. Onyx.connect({key: `${ONYXKEYS.COLLECTION.REPORT}{reportID}`, callback: ...}); */ - const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key); + const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key, collectionKeyLength); // Regular Onyx.connect() subscriber found. if (typeof subscriber.callback === 'function') { diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index c80bb853..406968ba 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -109,7 +109,11 @@ function useOnyx>(key: TKey const previousCollectionKey = OnyxUtils.splitCollectionMemberKey(previousKey)[0]; const collectionKey = OnyxUtils.splitCollectionMemberKey(key)[0]; - if (OnyxUtils.isCollectionMemberKey(previousCollectionKey, previousKey) && OnyxUtils.isCollectionMemberKey(collectionKey, key) && previousCollectionKey === collectionKey) { + if ( + OnyxUtils.isCollectionMemberKey(previousCollectionKey, previousKey, previousCollectionKey.length) && + OnyxUtils.isCollectionMemberKey(collectionKey, key, collectionKey.length) && + previousCollectionKey === collectionKey + ) { return; } } catch (e) { From eee10ec9816a43aa342b2cead8ca4a9adb2f3424 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Tue, 6 Aug 2024 13:09:30 +0500 Subject: [PATCH 3/3] perf: use startsWith on key --- lib/OnyxUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index 5f22b964..a5055cd1 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -395,7 +395,7 @@ function isCollectionKey(key: OnyxKey): key is CollectionKeyBase { } function isCollectionMemberKey(collectionKey: TCollectionKey, key: string, collectionKeyLength: number): key is `${TCollectionKey}${string}` { - return Str.startsWith(key, collectionKey) && key.length > collectionKeyLength; + return key.startsWith(collectionKey) && key.length > collectionKeyLength; } /**