Skip to content

Commit 1184c03

Browse files
committed
Improve performance of LRU cache
1 parent 284b003 commit 1184c03

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/reactor/cache.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ export class BasicCache {
9393
evict(item) {
9494
return new BasicCache(this.cache.remove(item))
9595
}
96+
97+
/**
98+
* Removes entry from cache
99+
* @param {Iterable} items
100+
* @return {BasicCache}
101+
*/
102+
evictMany(items) {
103+
const newCache = this.cache.withMutations(c => {
104+
items.forEach(item => {
105+
c.remove(item)
106+
})
107+
})
108+
109+
return new BasicCache(newCache)
110+
}
96111
}
97112

98113
const DEFAULT_LRU_LIMIT = 1000
@@ -173,15 +188,12 @@ export class LRUCache {
173188
)
174189
}
175190

176-
const cache = (this.lru
177-
.take(this.evictCount)
178-
.reduce((c, evictItem) => c.evict(evictItem), this.cache)
179-
.miss(item, entry))
191+
const itemsToRemove = this.lru.take(this.evictCount)
180192

181193
lruCache = new LRUCache(
182194
this.limit,
183195
this.evictCount,
184-
cache,
196+
this.cache.evictMany(itemsToRemove).miss(item, entry),
185197
this.lru.skip(this.evictCount).add(item)
186198
)
187199
} else {

0 commit comments

Comments
 (0)