16 lines
330 B
TypeScript
16 lines
330 B
TypeScript
export function pruneMapToMaxSize<K, V>(map: Map<K, V>, maxSize: number): void {
|
|
const limit = Math.max(0, Math.floor(maxSize));
|
|
if (limit <= 0) {
|
|
map.clear();
|
|
return;
|
|
}
|
|
|
|
while (map.size > limit) {
|
|
const oldest = map.keys().next();
|
|
if (oldest.done) {
|
|
break;
|
|
}
|
|
map.delete(oldest.value);
|
|
}
|
|
}
|