From 26fea01e29c527faf323c06f9864ad6c962c2caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Sat, 23 Sep 2023 12:08:25 +0200 Subject: [PATCH 1/2] don't recreate iterator every time --- lib/OnyxCache.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/OnyxCache.js b/lib/OnyxCache.js index f68315424..739e4a917 100644 --- a/lib/OnyxCache.js +++ b/lib/OnyxCache.js @@ -179,12 +179,23 @@ class OnyxCache { * Remove keys that don't fall into the range of recently used keys */ removeLeastRecentlyUsedKeys() { - while (this.recentKeys.size > this.maxRecentKeysSize) { - const iterator = this.recentKeys.values(); + let toRemove = this.recentKeys.size - this.maxRecentKeysSize; + if (toRemove <= 0) { + return; + } + const iterator = this.recentKeys.values(); + const temp = []; + while (toRemove > 0) { const value = iterator.next().value; if (value !== undefined) { - this.drop(value); + temp.push(value); } + toRemove--; + } + + for (let i = 0; i < temp.length; ++i) { + delete this.storageMap[temp[i]]; + this.recentKeys.delete(temp[i]); } } From a6af414982727a216b08ccce0727bf1780ab6972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Wed, 27 Sep 2023 13:34:12 +0200 Subject: [PATCH 2/2] apply suggestions --- lib/OnyxCache.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/OnyxCache.js b/lib/OnyxCache.js index 739e4a917..030ba0596 100644 --- a/lib/OnyxCache.js +++ b/lib/OnyxCache.js @@ -179,18 +179,16 @@ class OnyxCache { * Remove keys that don't fall into the range of recently used keys */ removeLeastRecentlyUsedKeys() { - let toRemove = this.recentKeys.size - this.maxRecentKeysSize; - if (toRemove <= 0) { + let numKeysToRemove = this.recentKeys.size - this.maxRecentKeysSize; + if (numKeysToRemove <= 0) { return; } const iterator = this.recentKeys.values(); const temp = []; - while (toRemove > 0) { + while (numKeysToRemove > 0) { const value = iterator.next().value; - if (value !== undefined) { - temp.push(value); - } - toRemove--; + temp.push(value); + numKeysToRemove--; } for (let i = 0; i < temp.length; ++i) {