Skip to content

Commit c5c8174

Browse files
committed
perf: avoid calculating collection key length and pass it as a const
1 parent dc00235 commit c5c8174

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/Onyx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ function updateSnapshots(data: OnyxUpdate[]) {
645645
const promises: Array<() => Promise<void>> = [];
646646

647647
const snapshotCollection = OnyxUtils.getCachedCollection(snapshotCollectionKey);
648+
const snapshotCollectionKeyLength = snapshotCollectionKey.length;
648649

649650
Object.entries(snapshotCollection).forEach(([snapshotKey, snapshotValue]) => {
650651
// 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[]) {
656657

657658
data.forEach(({key, value}) => {
658659
// snapshots are normal keys so we want to skip update if they are written to Onyx
659-
if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key)) {
660+
if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key, snapshotCollectionKeyLength)) {
660661
return;
661662
}
662663

lib/OnyxUtils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ function isCollectionKey(key: OnyxKey): key is CollectionKeyBase {
394394
return onyxCollectionKeySet.has(key);
395395
}
396396

397-
function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}` {
398-
return key.startsWith(collectionKey) && key.length > collectionKey.length;
397+
function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string, collectionKeyLength: number): key is `${TCollectionKey}${string}` {
398+
return Str.startsWith(key, collectionKey) && key.length > collectionKeyLength;
399399
}
400400

401401
/**
@@ -554,12 +554,14 @@ function getCachedCollection<TKey extends CollectionKeyBase>(collectionKey: TKey
554554
const allKeys = collectionMemberKeys || cache.getAllKeys();
555555
const collection: OnyxCollection<KeyValueMapping[TKey]> = {};
556556

557+
const collectionKeyLength = collectionKey.length;
558+
557559
// forEach exists on both Set and Array
558560
allKeys.forEach((key) => {
559561
// If we don't have collectionMemberKeys array then we have to check whether a key is a collection member key.
560562
// Because in that case the keys will be coming from `cache.getAllKeys()` and we need to filter out the keys that
561563
// are not part of the collection.
562-
if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key)) {
564+
if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key, collectionKeyLength)) {
563565
return;
564566
}
565567

@@ -595,6 +597,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
595597
// 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
596598
// 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().
597599
const stateMappingKeys = Object.keys(callbackToStateMapping);
600+
const collectionKeyLength = collectionKey.length;
598601
for (let i = 0; i < stateMappingKeys.length; i++) {
599602
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
600603
if (!subscriber) {
@@ -614,7 +617,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
614617
/**
615618
* e.g. Onyx.connect({key: `${ONYXKEYS.COLLECTION.REPORT}{reportID}`, callback: ...});
616619
*/
617-
const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key);
620+
const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key, collectionKeyLength);
618621

619622
// Regular Onyx.connect() subscriber found.
620623
if (typeof subscriber.callback === 'function') {

lib/useOnyx.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
109109
const previousCollectionKey = OnyxUtils.splitCollectionMemberKey(previousKey)[0];
110110
const collectionKey = OnyxUtils.splitCollectionMemberKey(key)[0];
111111

112-
if (OnyxUtils.isCollectionMemberKey(previousCollectionKey, previousKey) && OnyxUtils.isCollectionMemberKey(collectionKey, key) && previousCollectionKey === collectionKey) {
112+
if (
113+
OnyxUtils.isCollectionMemberKey(previousCollectionKey, previousKey, previousCollectionKey.length) &&
114+
OnyxUtils.isCollectionMemberKey(collectionKey, key, collectionKey.length) &&
115+
previousCollectionKey === collectionKey
116+
) {
113117
return;
114118
}
115119
} catch (e) {

0 commit comments

Comments
 (0)