TheAlgorithms/JavaScript · error · RangeError
LFUCache ERROR: The Capacity is 0
Error message
LFUCache ERROR: The Capacity is 0
What it means
LFUCache.set() refuses to store when the cache was constructed with capacity 0, because there is no room to hold any entry. The constructor permits 0 (a no-op cache), but set() treats it as a programmer error rather than silently dropping the value.
Source
Thrown at Cache/LFUCache.js:178
}
this.misses++
return null
}
/**
* @method set
* @description - This method stored the value by key & add frequency if it doesn't exist
* @param {string} key
* @param {any} value
* @param {number} frequency
* @returns {LFUCache}
*/
set(key, value, frequency = 1) {
key = String(key) // converted to string
if (this.#capacity === 0) {
throw new RangeError('LFUCache ERROR: The Capacity is 0')
}
if (this.cache.has(key)) {
const node = this.cache.get(key)
node.value = value
this.#frequencyMap.refresh(node)
return this
}
// if the cache size is full, then it's delete the Least Frequency Used node
if (this.#capacity === this.cache.size) {
this.#removeCacheNode()
}
const newNode = new CacheNode(key, value, frequency)
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Construct with capacity >= 1 if you intend to store values.
- Guard call sites: only call set() when cache.capacity > 0.
- Reconfigure capacity upward before inserting if it was set to 0 at startup.
Example fix
// before
const cache = new LFUCache(config.cacheSize ?? 0)
cache.set('k', v)
// after
const cache = new LFUCache(Math.max(1, config.cacheSize ?? 16))
cache.set('k', v) Defensive patterns
Strategy: validation
Validate before calling
function safeSet(cache, key, value) {
if (cache.capacity > 0) cache.set(key, value);
} Try / catch
try { cache.set(key, value); }
catch (e) {
if (e instanceof RangeError && /Capacity is 0/.test(e.message)) {
// cache disabled: skip or store elsewhere
} else throw e;
} Prevention
- Construct with capacity >= 1 whenever you will call set().
- Guard set() call sites with cache.capacity > 0.
- Treat capacity 0 as 'disabled' and short-circuit at the caller.
When it happens
Trigger: Constructing new LFUCache(0) (or resizing capacity to 0) and then calling .set(key, value).
Common situations: Reading capacity from a config that defaults to 0 when disabled, feature-flag disabling a cache but leaving set() calls active, or a size computed from data length that collapsed to 0.
Related errors
- Invalid capacity
- Capacity should be greater than 0
- Grid must be a non-empty array
- Both keyword and message must be specified
- Invalid keyword!
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/8ecc7b2a993f26c4.
Report an issue: GitHub.