88'use strict' ;
99
1010/**
11- * A Least Recently Used (LRU) cache implementation with optional TTL support.
11+ * A high-performance Least Recently Used (LRU) cache implementation with optional TTL support.
1212 * Items are automatically evicted when the cache reaches its maximum size,
13- * removing the least recently used items first.
13+ * removing the least recently used items first. All core operations (get, set, delete) are O(1).
1414 *
1515 * @class LRU
1616 * @example
2828class LRU {
2929 /**
3030 * Creates a new LRU cache instance.
31+ * Note: Constructor does not validate parameters. Use lru() factory function for parameter validation.
3132 *
3233 * @constructor
3334 * @param {number } [max=0] - Maximum number of items to store. 0 means unlimited.
3435 * @param {number } [ttl=0] - Time to live in milliseconds. 0 means no expiration.
35- * @param {boolean } [resetTtl=false] - Whether to reset TTL when accessing existing items.
36- * @throws {TypeError } When parameters are of invalid type.
36+ * @param {boolean } [resetTtl=false] - Whether to reset TTL when accessing existing items via get().
3737 * @example
3838 * const cache = new LRU(1000, 60000, true); // 1000 items, 1 minute TTL, reset on access
39+ * @see {@link lru } For parameter validation
3940 * @since 1.0.0
4041 */
4142 constructor ( max = 0 , ttl = 0 , resetTtl = false ) {
@@ -112,11 +113,12 @@ class LRU {
112113
113114 /**
114115 * Returns an array of [key, value] pairs for the specified keys.
116+ * Order follows LRU order (least to most recently used).
115117 *
116118 * @method entries
117119 * @memberof LRU
118120 * @param {string[] } [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.
119- * @returns {Array<Array<*>> } Array of [key, value] pairs.
121+ * @returns {Array<Array<*>> } Array of [key, value] pairs in LRU order .
120122 * @example
121123 * cache.set('a', 1).set('b', 2);
122124 * console.log(cache.entries()); // [['a', 1], ['b', 2]]
@@ -134,7 +136,7 @@ class LRU {
134136 *
135137 * @method evict
136138 * @memberof LRU
137- * @param {boolean } [bypass=false] - Whether to bypass the size check and force eviction .
139+ * @param {boolean } [bypass=false] - Whether to force eviction even when cache is empty .
138140 * @returns {LRU } The LRU instance for method chaining.
139141 * @example
140142 * cache.set('old', 'value').set('new', 'value');
@@ -243,11 +245,12 @@ class LRU {
243245
244246 /**
245247 * Efficiently moves an item to the end of the LRU list (most recently used position).
246- * This is much faster than calling set() for LRU position updates.
248+ * This is an internal optimization method that avoids the overhead of the full set() operation
249+ * when only LRU position needs to be updated.
247250 *
248251 * @method moveToEnd
249252 * @memberof LRU
250- * @param {Object } item - The item to move to the end .
253+ * @param {Object } item - The cache item with prev/next pointers to reposition .
251254 * @private
252255 * @since 11.3.5
253256 */
@@ -368,7 +371,7 @@ class LRU {
368371 * @memberof LRU
369372 * @param {string } key - The key to set.
370373 * @param {* } value - The value to store.
371- * @param {boolean } [bypass=false] - Whether to bypass normal LRU positioning (internal use) .
374+ * @param {boolean } [bypass=false] - Internal parameter for setWithEvicted method .
372375 * @param {boolean } [resetTtl=this.resetTtl] - Whether to reset the TTL for this operation.
373376 * @returns {LRU } The LRU instance for method chaining.
374377 * @example
@@ -420,11 +423,12 @@ class LRU {
420423
421424 /**
422425 * Returns an array of all values in the cache for the specified keys.
426+ * Order follows LRU order (least to most recently used).
423427 *
424428 * @method values
425429 * @memberof LRU
426430 * @param {string[] } [keys=this.keys()] - Array of keys to get values for. Defaults to all keys.
427- * @returns {Array<*> } Array of values corresponding to the keys.
431+ * @returns {Array<*> } Array of values corresponding to the keys in LRU order .
428432 * @example
429433 * cache.set('a', 1).set('b', 2);
430434 * console.log(cache.values()); // [1, 2]
@@ -439,14 +443,14 @@ class LRU {
439443}
440444
441445/**
442- * Factory function to create a new LRU cache instance with validation.
446+ * Factory function to create a new LRU cache instance with parameter validation.
443447 *
444448 * @function lru
445- * @param {number } [max=1000] - Maximum number of items to store. Must be >= 0.
446- * @param {number } [ttl=0] - Time to live in milliseconds. Must be >= 0.
447- * @param {boolean } [resetTtl=false] - Whether to reset TTL when accessing existing items.
449+ * @param {number } [max=1000] - Maximum number of items to store. Must be >= 0. Use 0 for unlimited size.
450+ * @param {number } [ttl=0] - Time to live in milliseconds. Must be >= 0. Use 0 for no expiration.
451+ * @param {boolean } [resetTtl=false] - Whether to reset TTL when accessing existing items via get() .
448452 * @returns {LRU } A new LRU cache instance.
449- * @throws {TypeError } When parameters are invalid.
453+ * @throws {TypeError } When parameters are invalid (negative numbers or wrong types) .
450454 * @example
451455 * // Create cache with factory function
452456 * const cache = lru(100, 5000, true);
0 commit comments