{"record":{"id":"8ecc7b2a993f26c4","repo":"TheAlgorithms/JavaScript","slug":"lfucache-error-the-capacity-is-0","errorCode":null,"errorMessage":"LFUCache ERROR: The Capacity is 0","messagePattern":"LFUCache ERROR: The Capacity is 0","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Cache/LFUCache.js","lineNumber":178,"sourceCode":"    }\n\n    this.misses++\n    return null\n  }\n\n  /**\n   * @method set\n   * @description - This method stored the value by key & add frequency if it doesn't exist\n   * @param {string} key\n   * @param {any} value\n   * @param {number} frequency\n   * @returns {LFUCache}\n   */\n  set(key, value, frequency = 1) {\n    key = String(key) // converted to string\n\n    if (this.#capacity === 0) {\n      throw new RangeError('LFUCache ERROR: The Capacity is 0')\n    }\n\n    if (this.cache.has(key)) {\n      const node = this.cache.get(key)\n      node.value = value\n\n      this.#frequencyMap.refresh(node)\n\n      return this\n    }\n\n    // if the cache size is full, then it's delete the Least Frequency Used node\n    if (this.#capacity === this.cache.size) {\n      this.#removeCacheNode()\n    }\n\n    const newNode = new CacheNode(key, value, frequency)\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Cache/LFUCache.js#L160-L196","documentation":"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.","triggerScenarios":"Constructing new LFUCache(0) (or resizing capacity to 0) and then calling .set(key, value).","commonSituations":"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.","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."],"exampleFix":"// before\nconst cache = new LFUCache(config.cacheSize ?? 0)\ncache.set('k', v)\n// after\nconst cache = new LFUCache(Math.max(1, config.cacheSize ?? 16))\ncache.set('k', v)","handlingStrategy":"validation","validationCode":"function safeSet(cache, key, value) {\n  if (cache.capacity > 0) cache.set(key, value);\n}","typeGuard":null,"tryCatchPattern":"try { cache.set(key, value); }\ncatch (e) {\n  if (e instanceof RangeError && /Capacity is 0/.test(e.message)) {\n    // cache disabled: skip or store elsewhere\n  } else throw e;\n}","preventionTips":["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."],"tags":["cache","lfu","capacity","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}