Skip to content

Commit

Permalink
perf: improve clear efficiency (#42)
Browse files Browse the repository at this point in the history
* perf: improve `clear` efficiency

* chore: improve scope
  • Loading branch information
wellwelwel authored Sep 20, 2024
1 parent d0b8727 commit e087a57
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,16 @@ export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {

/** Clears all key-value pairs from the cache. */
clear(): undefined {
for (const index of keyMap.values())
onEviction?.(keyList[index]!, valList[index]!);
if (typeof onEviction === 'function') {
const iterator = keyMap.values();

for (
let result = iterator.next();
!result.done;
result = iterator.next()
)
onEviction(keyList[result.value]!, valList[result.value]!);
}

keyMap.clear();
keyList.fill(undefined);
Expand Down

0 comments on commit e087a57

Please sign in to comment.