5 opts = Object.assign({}, opts);
7 if (!(opts.maxSize && opts.maxSize > 0)) {
8 throw new TypeError('`maxSize` must be a number greater than 0');
11 this.maxSize = opts.maxSize;
12 this.cache = new Map();
13 this.oldCache = new Map();
18 this.cache.set(key, value);
21 if (this._size >= this.maxSize) {
23 this.oldCache = this.cache;
24 this.cache = new Map();
29 if (this.cache.has(key)) {
30 return this.cache.get(key);
33 if (this.oldCache.has(key)) {
34 const value = this.oldCache.get(key);
35 this._set(key, value);
41 if (this.cache.has(key)) {
42 this.cache.set(key, value);
44 this._set(key, value);
51 return this.cache.has(key) || this.oldCache.has(key);
55 if (this.cache.has(key)) {
56 return this.cache.get(key);
59 if (this.oldCache.has(key)) {
60 return this.oldCache.get(key);
65 if (this.cache.delete(key)) {
69 this.oldCache.delete(key);
74 this.oldCache.clear();
79 for (const el of this) {
85 for (const el of this) {
90 * [Symbol.iterator]() {
91 for (const el of this.cache) {
95 for (const el of this.oldCache) {
96 if (!this.cache.has(el[0])) {
103 let oldCacheSize = 0;
104 for (const el of this.oldCache) {
105 if (!this.cache.has(el[0])) {
110 return this._size + oldCacheSize;
114 module.exports = QuickLRU;