Registry / storage / string-cache-map

string-cache-map

JSON →
library1.2.0jsnpmunverified

StringCacheMap (v1.2.0) is a high-performance, LRU-based Map implementation specifically optimized for string keys, designed as a drop-in replacement for WeakMap when keys are strings. It provides a WeakMap-compatible API (set, get, has, delete) with configurable capacity limits and a victim cache for fast LRU eviction. Unlike plain objects or Map, it avoids collisions with built-in properties like 'constructor' and ensures predictable memory usage. Released on npm with a stable API, it is suitable for caching scenarios where string keys are used and memory constraints exist. It offers a 'hard' mode (default) that strictly evicts oldest entries, and a 'soft' mode that preserves recently set values.

npm install string-cache-map
INSTALL
IMPORT
SIG · STRING-CACHE-MAP
S
string-cache-map
storagejavascriptv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import StringCacheMap from 'string-cache-map'
const { StringCacheMap } = require('string-cache-map')
Package exports a single default class; named import will fail. CommonJS users must use const StringCacheMap = require('string-cache-map').default or use the default export pattern.
StringCacheMap (type in TypeScript)
import type StringCacheMap from 'string-cache-map'
For TypeScript type imports, use this syntax to import only the type. The package does not ship types natively; install @types/string-cache-map if available.

Creates a StringCacheMap with a capacity limit of 2, adds three entries, and demonstrates LRU eviction behavior.

import StringCacheMap from 'string-cache-map'; const cache = new StringCacheMap(2); // limit to 2 entries cache.set('user1', { name: 'Alice' }); cache.set('user2', { name: 'Bob' }); console.log(cache.get('user1')); // { name: 'Alice' } console.log(cache.has('user3')); // false cache.set('user3', { name: 'Charlie' }); // evicts oldest (user2) console.log(cache.has('user2')); // false (evicted) console.log(cache.get('user3')); // { name: 'Charlie' }
Debug
Known issues
gotchaConstructor limit does not guarantee exactly N entries; the cache may temporarily hold up to 2*limit entries before eviction.
fix
Set limit to expected maximum capacity; for strict limits, use hard mode (default) and be aware of the double-buffer behavior.
affects: >=1.0.0
gotchaString type keys only; non-string keys will cause unexpected behavior (coercion to string or errors).
fix
Always pass strings as keys; manually convert keys with String(key) if needed.
affects: >=1.0.0
gotchaThe 'hard' mode (default) evicts last set values; soft mode (hard=false) retains newer values. Mode must be chosen at construction; cannot be changed later.
fix
Select appropriate mode when instantiating: new StringCacheMap(limit, hard).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Class extends value undefined is not a constructor or null
Trying to import the default export incorrectly with named import syntax.
fix
Use import StringCacheMap from 'string-cache-map' (default import) instead of import { StringCacheMap } from ...
Uncaught TypeError: StringCacheMap is not a constructor
Using require incorrectly: const { StringCacheMap } = require(...) returns undefined because there is no named export.
fix
Use const StringCacheMap = require('string-cache-map').default or const StringCacheMap = require('string-cache-map') if the package sets module.exports = default.
ReferenceError: StringCacheMap is not defined
Forgot to import the module.
fix
Add import StringCacheMap from 'string-cache-map' at the top of the file.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
20
Resources
string-cache-map — npm install string-cache-map · libregistry