{"name":"@asamuzakjp/generational-cache","version":"1.0.0","author":{"name":"asamuzaK"},"license":"MIT","_id":"@asamuzakjp/generational-cache@1.0.0","maintainers":[{"name":"asamuzakjp","email":"info@asamuzak.jp"}],"homepage":"https://github.com/asamuzaK/generationalCache","bugs":{"url":"https://github.com/asamuzaK/generationalCache/issues"},"dist":{"shasum":"8b38d03549d19eeee2af14bcd0e07c8165b3d56f","tarball":"http://registry-npm.ywwl.com/@asamuzakjp/generational-cache/download/@asamuzakjp/generational-cache-1.0.0.tgz","fileCount":5,"integrity":"sha512-TfnObbgAt7vpjieBiiYg8zKUIgE4WFWIiLLQmW+GA8UF+gv/jxI72jUBx7Pg2EFiRkR+gm+/Wgi+dGi9i4M9tQ==","signatures":[{"sig":"MEYCIQClZxxfbsycABJwJvHW9+3tQnYwFkLytEw+NS+hnQlK6QIhAMJjWLMdh7PrzrusJbYSWtCNSnFZe1zkbldg5Mh1B3fu","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":15565,"size":6014,"noattachment":false,"key":"/@asamuzakjp/generational-cache/-/@asamuzakjp/generational-cache-1.0.0.tgz"},"type":"module","engines":{"node":"^20.19.0 || ^22.12.0 || >=24.0.0"},"exports":{".":{"types":"./types/index.d.ts","default":"./src/index.js"},"./package.json":"./package.json"},"gitHead":"db5e7b1541d8378a8186f70ab69ba0351f341fc7","scripts":{"tsc":"node scripts/index clean --dir=types -i && npx tsc","lint":"eslint --fix .","test":"c8 --reporter=text mocha --exit test/*.test.js","bench":"node --expose-gc ./benchmark/benchmark.js","build":"npm run tsc && npm run lint && npm test","bench:worst":"node --expose-gc ./benchmark/worst-case-benchmark.js"},"_npmUser":{"name":"asamuzakjp","email":"info@asamuzak.jp"},"overrides":{"c8":{"yargs":"^18.0.0"},"eslint":{"brace-expansion":"^1.1.13"},"serialize-javascript":"^7.0.4"},"repository":{"url":"git+https://github.com/asamuzaK/generationalCache.git","type":"git"},"_npmVersion":"11.12.1","description":"A generational pseudo-LRU cache with strict maximum size limits.","directories":{},"_nodeVersion":"24.14.1","_hasShrinkwrap":false,"devDependencies":{"c8":"^11.0.0","mocha":"^11.7.5","eslint":"^9.39.4","mitata":"^1.0.34","globals":"^17.5.0","prettier":"^3.8.2","commander":"^14.0.3","lru-cache":"^11.3.5","mnemonist":"^0.40.3","quick-lru":"^7.3.0","typescript":"^6.0.2","@types/node":"^25.6.0","neostandard":"^0.13.0","eslint-plugin-jsdoc":"^62.9.0","eslint-plugin-regexp":"^3.1.0","eslint-plugin-unicorn":"^64.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-prettier":"^5.5.5"},"_npmOperationalInternal":{"tmp":"tmp/generational-cache_1.0.0_1776174324682_0.3405109213204798","host":"s3://npm-registry-packages-npm-production"},"readme":"# generational-cache\n\n[![CI](https://github.com/asamuzaK/generationalCache/actions/workflows/ci.yaml/badge.svg)](https://github.com/asamuzaK/generationalCache/actions/workflows/ci.yaml)\n[![CodeQL](https://github.com/asamuzaK/generationalCache/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/asamuzaK/generationalCache/actions/workflows/github-code-scanning/codeql)\n[![npm (scoped)](https://img.shields.io/npm/v/@asamuzakjp/generational-cache)](https://www.npmjs.com/package/@asamuzakjp/generational-cache)\n\nA lightweight, **generational cache** with strict entry-count limits and payload validation.\n\n## How it Works\n\n`GenerationalCache` maintains two internal `Map` objects: `current` and `old`.\nIt uses an internal boundary of ($max / 2$), allowing both generations to remain bounded while keeping generation swaps inexpensive.\n\n1. **Insertion & Validation**: New items are validated against byte size limits before being added to the `current` generation.\n2. **Promotion**: If you get an item that exists in the `old` generation, it is promoted to the `current` generation to ensure it stays in the cache longer.\n3. **Generation Swapping**: Once the `current` generation's size meets or exceeds the boundary threshold ($max / 2$), a generation swap is triggered: the existing `old` generation is discarded, the `current` generation becomes the new `old` generation, and a new empty `current` generation is created.\n\nThis two-generation approach avoids the overhead of updating timestamps or linked list pointers on every access. While not a drop-in replacement for standard LRU caches, it prioritizes raw throughput over strict eviction ordering.\n\n## When NOT to Use\n\n`GenerationalCache` may not be a good fit when:\n\n* You require strict LRU eviction ordering.\n* Cache hit rate is more important than insertion/eviction throughput.\n* You already know your working set size and can provision a sufficiently large LRU cache.\n\n## Installation\n\n```console\nnpm i @asamuzakjp/generational-cache\n```\n\n## Usage\n\n```javascript\nimport { GenerationalCache } from '@asamuzakjp/generational-cache';\n\n// Initialize with a max capacity of 1024 items and custom size limits\nconst cache = new GenerationalCache(1024, {\n  maxKeySize: 4096,          // 4 KB limit for keys\n  maxValueSize: 1024 * 1024, // 1 MB limit for values\n  strictValidate: true       // Enable strict deep validation for objects (default)\n});\n```\n\n## API\n\n### `new GenerationalCache(max, opt)`\n\nCreates a new cache instance with a maximum capacity of `max` entries.\n\n* **max** *(number)*: The maximum number of items the cache can hold. If the specified value is less than 4, or if an invalid value is specified, the default value of 4 will be used.\n* **opt** *(object, optional)*:\n  * **cacheFunction** *(boolean)*: Caches functions if `true`. Defaults to `false`.\n  * **cacheSymbol** *(boolean)*: Caches symbols if `true`. Defaults to `false`.\n  * **maxKeySize** *(number)*: Maximum allowed size for a `key` in bytes. Defaults to 8192 (8 KB).\n  * **maxValueSize** *(number)*: Maximum allowed size for a `value` in bytes. Defaults to 1048576 (1 MB).\n  * **strictValidate** *(boolean)*: Strictly validate object payload structures and sizes if `true`. Defaults to `true`.\n\n### Properties\n\n* **cache.entryCount** *(number, read-only)*: Returns the total number of underlying `entries` currently stored across both generations.\n  **Note:** To maximize write throughput, this library allows temporary key duplication between the `current` and `old` generations (e.g., when an item exists in both generations simultaneously). The reported count may exceed the number of unique `keys`, but remains bounded by the cache's internal capacity.\n* **cache.max** *(number)*: Gets or sets the maximum item capacity.\n  **Note:** Updating this property dynamically **will clear all existing cached items** (it implicitly invokes cache.clear() to safely recalculate boundaries).\n\n### Methods\n\n* **cache.get(key)**\n  Retrieves an item. If the item is found in the `old` generation, it is automatically promoted to the `current` generation to prevent it from being evicted during the next swap.\n  * **Returns** *(any | undefined)*: The value associated with the `key`, or `undefined` if the `key` is not found.\n* **cache.set(key, value)**\n  Adds or updates an item. It validates the byte size of both the key and value (if they are strings) before inserting. If this operation causes the `current` generation's size to meet or exceed the boundary threshold, a generation swap is triggered.\n  * **Note:** Storing `undefined` as a value **is not supported**, as cache.get(`key`) treats `undefined` as a cache miss.\n  * **Returns**: The cache instance itself (allows chaining).\n* **cache.has(key)**\n  Checks if a `key` exists in the cache (in either generation).\n  * **Returns:** `true` if the key exists, otherwise `false`.\n* **cache.delete(key)**\n  Removes an item from the cache.\n  * **Returns:** `true` if the item existed and was removed, otherwise `false`.\n* **cache.clear()**\n  Empties all items from the cache.\n\n## Performance\n\nBenchmarks are divided into two states to simulate real-world conditions:\n\n* **Cold State**: Measured with aggressive internal Garbage Collection to observe performance before full V8 TurboFan optimizations.\n* **Warm State**: Measured after sufficient warmup, representing sustained throughput under optimal JIT compilation.\n\n*The results below reflect the sustained operations per second (ops/sec), calculated from the average latency (ns/iter). Higher values indicate better performance.*\n\n### Benchmark Environment\n\n* **Engine:** Node.js v24.x (V8)\n* **Measurement:** [mitata](https://github.com/evanwashere/mitata).\n* **Comparison:** [LRUCache](https://www.npmjs.com/package/lru-cache) (v11.x), [QuickLRU](https://www.npmjs.com/package/quick-lru) (v7.x), [Mnemonist](https://www.npmjs.com/package/mnemonist) (v0.40.x)\n\n### 1. Small Cache (Max Size = 512)\n\n| Scenario | State | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- | :---- |\n| **Set** | Cold | 15,137,754 ops/sec | 4,265,484 ops/sec | 13,800,718 ops/sec | **17,844,397 ops/sec** |\n| | Warm | **18,993,352 ops/sec** | 15,299,878 ops/sec | **18,132,366 ops/sec** | **19,007,793 ops/sec** |\n| **Get** | Cold | 16,028,210 ops/sec | 7,566,013 ops/sec | 13,881,177 ops/sec | **30,030,030 ops/sec** |\n| | Warm | 17,917,936 ops/sec | 23,523,877 ops/sec | 16,458,196 ops/sec | **39,416,634 ops/sec** |\n| **Eviction** | Cold | **16,236,402 ops/sec** | 7,372,457 ops/sec | 14,547,571 ops/sec | 5,407,451 ops/sec |\n| | Warm | **20,479,214 ops/sec** | 8,993,615 ops/sec | 17,123,288 ops/sec | 7,668,712 ops/sec |\n\n### 2. Medium Cache (Max Size = 2,048)\n\n| Scenario | State | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- | :---- |\n| **Set** | Cold | **15,024,038 ops/sec** | 4,537,617 ops/sec | 11,818,934 ops/sec | **15,110,305 ops/sec** |\n| | Warm | **17,126,220 ops/sec** | 13,299,641 ops/sec | 15,309,247 ops/sec | **17,070,673 ops/sec** |\n| **Get** | Cold | 13,976,240 ops/sec | 8,134,711 ops/sec | 11,784,115 ops/sec | **26,315,789 ops/sec** |\n| | Warm | 16,015,375 ops/sec | 19,425,019 ops/sec | 13,599,891 ops/sec | **35,663,338 ops/sec** |\n| **Eviction** | Cold | **14,858,841 ops/sec** | 6,439,979 ops/sec | 12,425,447 ops/sec | 5,252,929 ops/sec |\n| | Warm | **19,805,902 ops/sec** | 7,981,483 ops/sec | 16,108,247 ops/sec | 7,295,010 ops/sec |\n\n### 3. Large Cache (Max Size = 8,192)\n\n| Scenario | State | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- | :---- |\n| **Set** | Cold | **13,292,569 ops/sec** | 3,637,686 ops/sec | 7,970,033 ops/sec | 11,318,619 ops/sec |\n| | Warm | **17,652,251 ops/sec** | 11,709,602 ops/sec | 12,781,186 ops/sec | 15,225,335 ops/sec |\n| **Get** | Cold | 13,840,830 ops/sec | 5,746,136 ops/sec | 11,021,713 ops/sec | **17,155,601 ops/sec** |\n| | Warm | 19,116,804 ops/sec | 17,885,888 ops/sec | 15,664,160 ops/sec | **28,344,671 ops/sec** |\n| **Eviction** | Cold | **14,025,245 ops/sec** | 5,358,770 ops/sec | 9,882,399 ops/sec | 4,185,501 ops/sec |\n| | Warm | **19,249,278 ops/sec** | 7,029,383 ops/sec | 13,646,288 ops/sec | 6,086,798 ops/sec |\n\n### 4. Non-Primitive Payload (Max Size = 4,096 / strictValidate = true)\n\n| Scenario | State | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- | :---- |\n| **Set** | Cold | 602,410 ops/sec | 3,003,725 ops/sec | 9,948,269 ops/sec | **10,231,226 ops/sec** |\n| | Warm | 757,576 ops/sec | 10,964,912 ops/sec | 14,697,237 ops/sec | **16,572,754 ops/sec** |\n| **Get** | Cold | 13,180,440 ops/sec | 7,043,742 ops/sec | 11,037,528 ops/sec | **17,580,872 ops/sec** |\n| | Warm | 16,170,763 ops/sec | 19,007,793 ops/sec | 14,363,689 ops/sec | **28,719,127 ops/sec** |\n| **Eviction** | Cold | 617,284 ops/sec | 5,356,186 ops/sec | **12,072,920 ops/sec** | 2,607,222 ops/sec |\n| | Warm | 775,194 ops/sec | 7,504,127 ops/sec | **14,981,273 ops/sec** | 6,208,481 ops/sec |\n\n### 5. Non-Primitive Payload (Max Size = 4,096 / strictValidate = false)\n\n| Scenario | State | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- | :---- |\n| **Set** | Cold | **12,315,271 ops/sec** | 2,650,060 ops/sec | 10,233,320 ops/sec | 10,884,946 ops/sec |\n| | Warm | **16,100,467 ops/sec** | 10,561,893 ops/sec | 14,624,159 ops/sec | **16,672,224 ops/sec** |\n| **Get** | Cold | 13,835,086 ops/sec | 7,248,478 ops/sec | 10,714,668 ops/sec | **19,015,022 ops/sec** |\n| | Warm | 16,012,810 ops/sec | 18,744,142 ops/sec | 14,106,362 ops/sec | **32,927,231 ops/sec** |\n| **Eviction** | Cold | 10,424,268 ops/sec | 5,473,753 ops/sec | **11,983,223 ops/sec** | 2,544,853 ops/sec |\n| | Warm | **16,291,952 ops/sec** | 6,972,996 ops/sec | 13,417,416 ops/sec | 5,798,782 ops/sec |\n\n### 6. Cyclic Access (Max Size = 8,192 / Working Set = 5,000)\n\n| Metric | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- |\n| **Hit Rate** | 78.30% | **100.00%** | **100.00%** | **100.00%** |\n| **Throughput** | 8,783,487 ops/sec | 37,778,617 ops/sec | 38,550,501 ops/sec | **44,742,729 ops/sec** |\n\n### 7. Cyclic Access (Max Size = 4,096 / Working Set = 5,000)\n\n| Metric | GenerationalCache | LRUCache | QuickLRU | Mnemonist |\n| :---- | :---- | :---- | :---- | :---- |\n| **Hit Rate** | 0.00% | 0.00% | **78.30%** | 0.00% |\n| **Throughput** | **9,931,472 ops/sec** | 6,396,724 ops/sec | 8,509,189 ops/sec | 6,078,288 ops/sec |\n\n## Key Characteristics\n\n* **High Eviction Efficiency**: `GenerationalCache` demonstrates strong throughput during high-turnover workloads, maintaining a performance margin compared to standard LRU designs in large-scale eviction scenarios.\n* **Predictable Scalability**: While other libraries may experience performance degradation as cache size increases, `GenerationalCache` maintains consistent throughput due to its generational swap mechanism.\n* **Balanced Read/Write**: It provides stable and competitive performance across all basic operations (`get`, `set`), making it suitable for both read-heavy and write-heavy environments.\n* **Strict Validation Toggle**: By default, non-primitive payloads undergo deep validation to prevent memory exhaustion from oversized objects, which impacts write throughput. Disabling `strictValidate` restores write performance, provided that payload sizes are managed externally (See 4 & 5).\n* **Trade-offs**: In cyclic access patterns where the working set is greater than `max / 2` but smaller than `max`, `GenerationalCache` will experience frequent generation swaps and cache misses. To maximize the performance benefits of `GenerationalCache`, it is often better to keep the `max` size small enough to allow some evictions, rather than trying to fit the entire working set (See 6 & 7).\n\n## License\n\nMIT\n","publish_time":1776174324819,"_cnpm_publish_time":1776174324819,"dist-tags":{"latest":"3.0.1","next":"0.1.0"}}