From 61aa8101c41853b68edc63cb1af26f78c2ecf3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Fri, 22 Sep 2023 10:49:39 +0200 Subject: [PATCH 1/2] make remove key O(log N) instead of O(N^2) --- lib/OnyxCache.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/OnyxCache.js b/lib/OnyxCache.js index e7f6f58fb..73af434de 100644 --- a/lib/OnyxCache.js +++ b/lib/OnyxCache.js @@ -179,16 +179,13 @@ class OnyxCache { * Remove keys that don't fall into the range of recently used keys */ removeLeastRecentlyUsedKeys() { - if (this.recentKeys.size <= this.maxRecentKeysSize) { - return; + while (this.recentKeys.size > this.maxRecentKeysSize) { + const iterator = this.recentKeys.values(); + const value = iterator.next().value + if (value !== undefined) { + this.drop(value) + } } - - // Get the last N keys by doing a negative slice - const recentlyAccessed = [...this.recentKeys].slice(-this.maxRecentKeysSize); - const storageKeys = _.keys(this.storageMap); - const keysToRemove = _.difference(storageKeys, recentlyAccessed); - - _.each(keysToRemove, this.drop); } /** From 4927b0fb0ae20460fb277c3c802bc13aa5639bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Fri, 22 Sep 2023 10:59:52 +0200 Subject: [PATCH 2/2] fix lint --- lib/OnyxCache.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OnyxCache.js b/lib/OnyxCache.js index 73af434de..f68315424 100644 --- a/lib/OnyxCache.js +++ b/lib/OnyxCache.js @@ -181,9 +181,9 @@ class OnyxCache { removeLeastRecentlyUsedKeys() { while (this.recentKeys.size > this.maxRecentKeysSize) { const iterator = this.recentKeys.values(); - const value = iterator.next().value + const value = iterator.next().value; if (value !== undefined) { - this.drop(value) + this.drop(value); } } }