From 768bf3ae95b9b3ac5b88bfdde759c0b9313d2bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Miko=C5=82ajczak?= Date: Tue, 23 Jul 2024 07:19:30 +0200 Subject: [PATCH] ArrayCache tweaks --- src/libs/memoize/cache/ArrayCache.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/memoize/cache/ArrayCache.ts b/src/libs/memoize/cache/ArrayCache.ts index fd21210dea90..058efefdb1aa 100644 --- a/src/libs/memoize/cache/ArrayCache.ts +++ b/src/libs/memoize/cache/ArrayCache.ts @@ -10,8 +10,11 @@ function ArrayCache(config: CacheConfig): Cache { const {maxSize, keyComparator} = config; - function getKeyIndex(key: K) { - // We search the array backwards because the most recently added entries are at the end, and our heuristic follows the principles of an LRU cache - that the most recently added entries are most likely to be used again. + /** + * Returns the index of the key in the cache array. + * We search the array backwards because the most recently added entries are at the end, and our heuristic follows the principles of an LRU cache - that the most recently added entries are most likely to be used again. + */ + function getKeyIndex(key: K): number { for (let i = cache.length - 1; i >= 0; i--) { if (keyComparator(cache[i][0], key)) { return i; @@ -25,7 +28,7 @@ function ArrayCache(config: CacheConfig): Cache { const index = getKeyIndex(key); if (index === -1) { - return; + return undefined; } const [entry] = cache.splice(index, 1); @@ -47,7 +50,7 @@ function ArrayCache(config: CacheConfig): Cache { } }, - getSet(key: K, valueProducer: () => V) { + getSet(key, valueProducer) { const index = getKeyIndex(key); if (index !== -1) {