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); 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; } }