Skip to content

Commit 845f01e

Browse files
committed
Updating docblocks
1 parent 9c7e916 commit 845f01e

7 files changed

Lines changed: 86 additions & 70 deletions

File tree

benchmarks/comparison-benchmark.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ async function runBenchmarks () {
258258
deleteBench.table();
259259

260260
// UPDATE operations benchmark
261-
console.log('\n📊 UPDATE Operations Benchmark');
262-
console.log('=' .repeat(50));
261+
console.log("\n📊 UPDATE Operations Benchmark");
262+
console.log("=" .repeat(50));
263263

264264
const updateBench = new Bench({ time: 2000 });
265265

266266
updateBench
267-
.add('tiny-lru update', () => {
267+
.add("tiny-lru update", () => {
268268
const cache = tinyLru(CACHE_SIZE);
269269
// Pre-populate with initial values
270270
for (let i = 0; i < 100; i++) {
@@ -275,7 +275,7 @@ async function runBenchmarks () {
275275
cache.set(testData.keys[i], testData.values[(i + 50) % testData.values.length]);
276276
}
277277
})
278-
.add('tiny-lru-ttl update', () => {
278+
.add("tiny-lru-ttl update", () => {
279279
const cache = tinyLru(CACHE_SIZE, TTL_MS);
280280
// Pre-populate with initial values
281281
for (let i = 0; i < 100; i++) {
@@ -286,7 +286,7 @@ async function runBenchmarks () {
286286
cache.set(testData.keys[i], testData.values[(i + 50) % testData.values.length]);
287287
}
288288
})
289-
.add('lru-cache update', () => {
289+
.add("lru-cache update", () => {
290290
const cache = new LRUCache({ max: CACHE_SIZE });
291291
// Pre-populate with initial values
292292
for (let i = 0; i < 100; i++) {
@@ -297,7 +297,7 @@ async function runBenchmarks () {
297297
cache.set(testData.keys[i], testData.values[(i + 50) % testData.values.length]);
298298
}
299299
})
300-
.add('lru-cache-ttl update', () => {
300+
.add("lru-cache-ttl update", () => {
301301
const cache = new LRUCache({ max: CACHE_SIZE, ttl: TTL_MS });
302302
// Pre-populate with initial values
303303
for (let i = 0; i < 100; i++) {
@@ -308,7 +308,7 @@ async function runBenchmarks () {
308308
cache.set(testData.keys[i], testData.values[(i + 50) % testData.values.length]);
309309
}
310310
})
311-
.add('quick-lru update', () => {
311+
.add("quick-lru update", () => {
312312
const cache = new QuickLRU({ maxSize: CACHE_SIZE });
313313
// Pre-populate with initial values
314314
for (let i = 0; i < 100; i++) {
@@ -319,7 +319,7 @@ async function runBenchmarks () {
319319
cache.set(testData.keys[i], testData.values[(i + 50) % testData.values.length]);
320320
}
321321
})
322-
.add('mnemonist update', () => {
322+
.add("mnemonist update", () => {
323323
const cache = new MnemonistLRU(CACHE_SIZE);
324324
// Pre-populate with initial values
325325
for (let i = 0; i < 100; i++) {

dist/tiny-lru.cjs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
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
@@ -28,14 +28,15 @@
2828
class 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);

dist/tiny-lru.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* @version 11.3.4
77
*/
88
/**
9-
* A Least Recently Used (LRU) cache implementation with optional TTL support.
9+
* A high-performance Least Recently Used (LRU) cache implementation with optional TTL support.
1010
* Items are automatically evicted when the cache reaches its maximum size,
11-
* removing the least recently used items first.
11+
* removing the least recently used items first. All core operations (get, set, delete) are O(1).
1212
*
1313
* @class LRU
1414
* @example
@@ -26,14 +26,15 @@
2626
class LRU {
2727
/**
2828
* Creates a new LRU cache instance.
29+
* Note: Constructor does not validate parameters. Use lru() factory function for parameter validation.
2930
*
3031
* @constructor
3132
* @param {number} [max=0] - Maximum number of items to store. 0 means unlimited.
3233
* @param {number} [ttl=0] - Time to live in milliseconds. 0 means no expiration.
33-
* @param {boolean} [resetTtl=false] - Whether to reset TTL when accessing existing items.
34-
* @throws {TypeError} When parameters are of invalid type.
34+
* @param {boolean} [resetTtl=false] - Whether to reset TTL when accessing existing items via get().
3535
* @example
3636
* const cache = new LRU(1000, 60000, true); // 1000 items, 1 minute TTL, reset on access
37+
* @see {@link lru} For parameter validation
3738
* @since 1.0.0
3839
*/
3940
constructor (max = 0, ttl = 0, resetTtl = false) {
@@ -110,11 +111,12 @@ class LRU {
110111

111112
/**
112113
* Returns an array of [key, value] pairs for the specified keys.
114+
* Order follows LRU order (least to most recently used).
113115
*
114116
* @method entries
115117
* @memberof LRU
116118
* @param {string[]} [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.
117-
* @returns {Array<Array<*>>} Array of [key, value] pairs.
119+
* @returns {Array<Array<*>>} Array of [key, value] pairs in LRU order.
118120
* @example
119121
* cache.set('a', 1).set('b', 2);
120122
* console.log(cache.entries()); // [['a', 1], ['b', 2]]
@@ -132,7 +134,7 @@ class LRU {
132134
*
133135
* @method evict
134136
* @memberof LRU
135-
* @param {boolean} [bypass=false] - Whether to bypass the size check and force eviction.
137+
* @param {boolean} [bypass=false] - Whether to force eviction even when cache is empty.
136138
* @returns {LRU} The LRU instance for method chaining.
137139
* @example
138140
* cache.set('old', 'value').set('new', 'value');
@@ -241,11 +243,12 @@ class LRU {
241243

242244
/**
243245
* Efficiently moves an item to the end of the LRU list (most recently used position).
244-
* This is much faster than calling set() for LRU position updates.
246+
* This is an internal optimization method that avoids the overhead of the full set() operation
247+
* when only LRU position needs to be updated.
245248
*
246249
* @method moveToEnd
247250
* @memberof LRU
248-
* @param {Object} item - The item to move to the end.
251+
* @param {Object} item - The cache item with prev/next pointers to reposition.
249252
* @private
250253
* @since 11.3.5
251254
*/
@@ -366,7 +369,7 @@ class LRU {
366369
* @memberof LRU
367370
* @param {string} key - The key to set.
368371
* @param {*} value - The value to store.
369-
* @param {boolean} [bypass=false] - Whether to bypass normal LRU positioning (internal use).
372+
* @param {boolean} [bypass=false] - Internal parameter for setWithEvicted method.
370373
* @param {boolean} [resetTtl=this.resetTtl] - Whether to reset the TTL for this operation.
371374
* @returns {LRU} The LRU instance for method chaining.
372375
* @example
@@ -418,11 +421,12 @@ class LRU {
418421

419422
/**
420423
* Returns an array of all values in the cache for the specified keys.
424+
* Order follows LRU order (least to most recently used).
421425
*
422426
* @method values
423427
* @memberof LRU
424428
* @param {string[]} [keys=this.keys()] - Array of keys to get values for. Defaults to all keys.
425-
* @returns {Array<*>} Array of values corresponding to the keys.
429+
* @returns {Array<*>} Array of values corresponding to the keys in LRU order.
426430
* @example
427431
* cache.set('a', 1).set('b', 2);
428432
* console.log(cache.values()); // [1, 2]
@@ -437,14 +441,14 @@ class LRU {
437441
}
438442

439443
/**
440-
* Factory function to create a new LRU cache instance with validation.
444+
* Factory function to create a new LRU cache instance with parameter validation.
441445
*
442446
* @function lru
443-
* @param {number} [max=1000] - Maximum number of items to store. Must be >= 0.
444-
* @param {number} [ttl=0] - Time to live in milliseconds. Must be >= 0.
445-
* @param {boolean} [resetTtl=false] - Whether to reset TTL when accessing existing items.
447+
* @param {number} [max=1000] - Maximum number of items to store. Must be >= 0. Use 0 for unlimited size.
448+
* @param {number} [ttl=0] - Time to live in milliseconds. Must be >= 0. Use 0 for no expiration.
449+
* @param {boolean} [resetTtl=false] - Whether to reset TTL when accessing existing items via get().
446450
* @returns {LRU} A new LRU cache instance.
447-
* @throws {TypeError} When parameters are invalid.
451+
* @throws {TypeError} When parameters are invalid (negative numbers or wrong types).
448452
* @example
449453
* // Create cache with factory function
450454
* const cache = lru(100, 5000, true);

0 commit comments

Comments
 (0)