Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
89 changes: 49 additions & 40 deletions src/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down
Loading