puzzlescript
Version:
Play PuzzleScript games in your terminal!
45 lines • 1.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class QuickLru {
constructor(maxSize) {
this.maxSize = maxSize;
this.size = 0;
this.cache = {};
this.oldCache = {};
}
set(key, value) {
const has = typeof this.cache[key] !== 'undefined' || typeof this.oldCache[key] !== 'undefined';
if (this.size > this.maxSize) {
this.oldCache = this.cache;
this.size = 0;
}
if (!has) {
this.cache[key] = value;
this.size++;
}
}
get(key) {
const value = this.cache[key];
if (typeof value !== 'undefined') {
return value;
}
return this.oldCache[key];
}
}
class LruCache {
constructor(maxSize) {
this.lru = new QuickLru(maxSize);
}
get(key, valueFn) {
const val = this.lru.get(key);
// speed up by combining .has(key) and .get(key)
if (val !== undefined) {
return val;
}
const value = valueFn();
this.lru.set(key, value);
return value;
}
}
exports.default = LruCache;
//# sourceMappingURL=lruCache.js.map