Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Last active Jul 31, 2020
Embed
What would you like to do?
// Check and trim our cache size
#resizeMap = () => {
if (this.size <= this.#maxSize)
return false;
debug(`Resizing map: ${this.size} > ${this.#maxSize}`);
// Elements required to determine candidates
let minKey;
let minFrequency = Number.MAX_VALUE;
let minTime;
// Find the lowest frequency key
for (let [key, value] of this.#frequencyMap) {
let entryFrequency = value.frequency;
let entryTime = value.recency;
// Find the lowest frequency key
if (entryFrequency < minFrequency) {
minFrequency = entryFrequency;
minKey = key;
minTime = entryTime;
} else {
// If there are multiple keys with same frequency
// Choose the least recency key
if (entryFrequency == minFrequency) {
if (entryTime < minTime) {
minFrequency = entryFrequency;
minKey = key;
minTime = entryTime;
}
}
}
}
debug(`Deleting { ${minKey} => { frequency: ${minFrequency}, recency: ${minTime} }`);
this.delete(minKey);
this.#frequencyMap.delete(minkey);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment