Once maxSize is reached calculations don't account for duplicates
lukechilds opened this issue · 1 comments
lukechilds commented
Because there are two Maps when you overflow maxSize
you can't rely on all of the items to be unique.
This means lru.size
is incorrect, it will count duplicates when it should only count each key once.
This also means the iterables are incorrect.
e.g:
const lru = new QuickLRU({ maxSize: 2 });
lru.set('key', 'value');
lru.set('shouldOnlyAppearOnce', 1);
lru.set('shouldOnlyAppearOnce', 2);
lru.size;
// 3
for(keyValue of lru) {
console.log(keyValue);
}
// [ 'shouldOnlyAppearOnce', 2 ]
// [ 'key', 'value' ]
// [ 'shouldOnlyAppearOnce', 1 ]
Same issue affects generator functions for lru.keys
and lru.values
.
lukechilds commented
Should probably clarify, all other methods are unaffected.