{"record":{"id":"5924df8e47da4591","repo":"TheAlgorithms/JavaScript","slug":"capacity-should-be-greater-than-0","errorCode":null,"errorMessage":"Capacity should be greater than 0","messagePattern":"Capacity should be greater than 0","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Cache/LRUCache.js","lineNumber":41,"sourceCode":"    return Object.freeze({\n      misses: this.misses,\n      hits: this.hits,\n      capacity: this.capacity,\n      size: this.size\n    })\n  }\n\n  get size() {\n    return this.cache.size\n  }\n\n  get capacity() {\n    return this.#capacity\n  }\n\n  set capacity(newCapacity) {\n    if (newCapacity < 0) {\n      throw new RangeError('Capacity should be greater than 0')\n    }\n\n    if (newCapacity < this.capacity) {\n      let diff = this.capacity - newCapacity\n\n      while (diff--) {\n        this.#removeLeastRecentlyUsed()\n      }\n    }\n\n    this.#capacity = newCapacity\n  }\n\n  /**\n   * delete oldest key existing in map by the help of iterator\n   */\n  #removeLeastRecentlyUsed() {\n    this.cache.delete(this.cache.keys().next().value)","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Cache/LRUCache.js#L23-L59","documentation":"Thrown by the LRUCache capacity setter when the new value is negative. Note the guard is `newCapacity < 0` but the message reads 'greater than 0', so the message is slightly misleading: 0 is actually accepted (it just empties the cache via eviction until capacity is reached), only negatives throw.","triggerScenarios":"cache.capacity = -5, or assigning a computed size that goes negative (e.g. after subtracting a budget).","commonSituations":"Dynamically resizing the cache from a metric that dipped below zero, or a subtraction underflow when shrinking.","solutions":["Assign a non-negative value: cache.capacity = Math.max(0, newSize).","Clamp computed sizes before assignment.","Be aware 0 is permitted and will evict everything; the guard only blocks negatives."],"exampleFix":"// before\ncache.capacity = currentLoad - reserved // can be negative\n// after\ncache.capacity = Math.max(0, currentLoad - reserved)","handlingStrategy":"validation","validationCode":"function resizeLRU(cache, newSize) {\n  cache.capacity = Math.max(0, newSize);\n}","typeGuard":null,"tryCatchPattern":"try { cache.capacity = newSize; }\ncatch (e) {\n  if (e instanceof RangeError && /greater than 0/.test(e.message)) {\n    cache.capacity = Math.max(0, newSize);\n  } else throw e;\n}","preventionTips":["Clamp computed sizes with Math.max(0, x) before assigning.","Remember the guard blocks negatives only; 0 evicts everything.","Validate newSize >= 0 at the source of the resize request."],"tags":["cache","lru","capacity","range-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}