diff --git a/coverage.txt b/coverage.txt index 8af241a..fd56314 100644 --- a/coverage.txt +++ b/coverage.txt @@ -3,8 +3,8 @@ ℹ file | line % | branch % | funcs % | uncovered lines ℹ ---------------------------------------------------------- ℹ src | | | | -ℹ lru.js | 100.00 | 99.37 | 100.00 | +ℹ lru.js | 100.00 | 99.36 | 100.00 | ℹ ---------------------------------------------------------- -ℹ all files | 100.00 | 99.37 | 100.00 | +ℹ all files | 100.00 | 99.36 | 100.00 | ℹ ---------------------------------------------------------- ℹ end of coverage report diff --git a/src/lru.js b/src/lru.js index e82a76b..ae6b413 100644 --- a/src/lru.js +++ b/src/lru.js @@ -96,11 +96,7 @@ export class LRU { entries(keys) { if (keys === undefined) { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push([x.key, x.value]); - } - } + this.#forEachLive((x) => result.push([x.key, x.value])); return result; } @@ -289,6 +285,51 @@ export class LRU { return item; } + /** + * Creates and links a new item at the most recently used position. + * Wires the first/last pointers and returns the created item. + * + * @param {string} key - The key to store. + * @param {*} value - The value to store. + * @returns {Object} The created and linked item. + * @private + */ + #insertItem(key, value) { + const item = (this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }); + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; + + return item; + } + + /** + * Invokes a callback for each live (non-expired) item in LRU order. + * Accesses items directly without calling get(), so LRU order is not + * mutated and expired items are not deleted mid-iteration. + * + * @param {function(Object): void} callback - Function to call for each live item. + * @private + */ + #forEachLive(callback) { + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + callback(x); + } + } + } + /** * Efficiently moves an item to the end of the LRU list (most recently used position). * This is an internal optimization method that avoids the overhead of the full set() operation @@ -318,11 +359,7 @@ export class LRU { */ keys() { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push(x.key); - } - } + this.#forEachLive((x) => result.push(x.key)); return result; } @@ -359,21 +396,7 @@ export class LRU { }; } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; @@ -407,21 +430,7 @@ export class LRU { this.evict(); } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++;