Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use one loop instead of three for getCachedCollection #546

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,22 @@ function addAllSafeEvictionKeysToRecentlyAccessedList(): Promise<void> {
}

function getCachedCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collectionMemberKeys?: string[]): NonNullable<OnyxCollection<KeyValueMapping[TKey]>> {
const resolvedCollectionMemberKeys = collectionMemberKeys || Array.from(cache.getAllKeys()).filter((storedKey) => isCollectionMemberKey(collectionKey, storedKey));

return resolvedCollectionMemberKeys.reduce((prev: NonNullable<OnyxCollection<KeyValueMapping[TKey]>>, key) => {
const cachedValue = cache.getValue(key);
if (!cachedValue) {
return prev;
const allKeys = collectionMemberKeys || cache.getAllKeys();
const collection: OnyxCollection<KeyValueMapping[TKey]> = {};

// 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)) {
return;
}

// eslint-disable-next-line no-param-reassign
prev[key] = cachedValue;
return prev;
}, {});
collection[key] = cache.getValue(key);
});

return collection;
}

/**
Expand Down
Loading