From 2693ce66c4ffc72fc9b23c3b53b22ca8a042a952 Mon Sep 17 00:00:00 2001 From: francesco Date: Fri, 9 Jan 2026 16:33:15 +0100 Subject: [PATCH 1/2] perf: optimize entries, keys, and values methods --- src/lru.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lru.js b/src/lru.js index 7c107a2..8ed976d 100644 --- a/src/lru.js +++ b/src/lru.js @@ -119,7 +119,13 @@ export class LRU { * @since 11.1.0 */ entries (keys = this.keys()) { - return keys.map(key => [key, this.get(key)]); + const result = new Array(keys.length); + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + result[i] = [key, this.get(key)]; + } + + return result; } /** @@ -296,11 +302,12 @@ export class LRU { * @since 9.0.0 */ keys () { - const result = []; + const result = new Array(this.size); let x = this.first; + let i = 0; while (x !== null) { - result.push(x.key); + result[i++] = x.key; x = x.next; } @@ -429,7 +436,12 @@ export class LRU { * @since 11.1.0 */ values (keys = this.keys()) { - return keys.map(key => this.get(key)); + const result = new Array(keys.length); + for (let i = 0; i < keys.length; i++) { + result[i] = this.get(keys[i]); + } + + return result; } } From bcd70178f9b92821abec74cb37ff0e7ca11754a5 Mon Sep 17 00:00:00 2001 From: francesco Date: Fri, 9 Jan 2026 16:43:23 +0100 Subject: [PATCH 2/2] fix: change LRUCache to LRUCacheWithDelete import --- benchmarks/comparison-benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/comparison-benchmark.js b/benchmarks/comparison-benchmark.js index 15f9d41..69cb2cd 100644 --- a/benchmarks/comparison-benchmark.js +++ b/benchmarks/comparison-benchmark.js @@ -30,7 +30,7 @@ try { try { // Import from mnemonist using the correct export pattern const mnemonistModule = await import("mnemonist"); - MnemonistLRU = mnemonistModule.LRUCache; + MnemonistLRU = mnemonistModule.LRUCacheWithDelete; } catch (error) { console.error("mnemonist not found. Run: npm install --no-save mnemonist"); console.error("Error:", error.message);