This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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