minor performance upgrade

This commit is contained in:
Schluffe
2026-04-07 23:32:22 +02:00
parent 03f4119e8c
commit c349dcec29
4 changed files with 46 additions and 34 deletions
+37
View File
@@ -0,0 +1,37 @@
import type { Box } from "#/engine/Box.ts";
export default class MapArray<K, V> {
map: Map<K, Box<V>>;
array: Array<[K, Box<V>]>;
constructor(iterable?: Iterable<[K, Box<V>]>) {
this.map = new Map(iterable);
this.array = iterable ? [...iterable] : [];
}
delete(key: K): void {
if (this.map.delete(key)) {
this.array[this.array.findIndex((e) => e[0] === key)] =
this.array[this.array.length - 1];
this.array.pop();
}
}
set(key: K, value: Box<V>): void {
if (this.map.has(key)) {
this.array.find((e) => e[0] === key)![1] = value;
} else {
this.array.push([key, value]);
}
this.map.set(key, value);
}
get(key: K): Box<V> | undefined {
return this.map.get(key);
}
entries(): Array<[K, Box<V>]> {
return this.array;
}
}
-25
View File
@@ -1,25 +0,0 @@
import type { Box } from "#/engine/Box.ts";
export default class MapProxy<K, V> {
map: Map<K, Box<V>>;
constructor(iterable?: Iterable<readonly [K, Box<V>]>) {
this.map = new Map(iterable);
}
get(key: K): Box<V> | undefined {
return this.map.get(key);
}
entries(): MapIterator<[K, Box<V>]> {
return this.map.entries();
}
keys(): MapIterator<K> {
return this.map.keys();
}
values(): MapIterator<Box<V>> {
return this.map.values();
}
}