Skip to content

Commit d313455

Browse files
committed
Faster call
1 parent 9db16c8 commit d313455

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

src/polyfills.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ if (typeof Math.sumPrecise !== "function") {
2727
// https://github.com/tc39/proposal-upsert
2828
if (typeof Map.prototype.getOrInsert === "undefined") {
2929
Map.prototype.getOrInsert = function <K, V>(this: Map<K, V>, key: K, defaultValue: V): V {
30-
if (!this.has(key)) {
31-
this.set(key, defaultValue);
30+
const v = this.get(key);
31+
if (v !== undefined) {
32+
return v;
3233
}
33-
// biome-ignore lint/style/noNonNullAssertion: inserted bevore
34-
return this.get(key)!;
34+
35+
this.set(key, defaultValue);
36+
return defaultValue;
3537
};
3638
}
3739
if (typeof Map.prototype.getOrInsertComputed === "undefined") {
@@ -40,10 +42,13 @@ if (typeof Map.prototype.getOrInsertComputed === "undefined") {
4042
key: K,
4143
callbackFunction: (key: K) => V,
4244
): V {
43-
if (!this.has(key)) {
44-
this.set(key, callbackFunction(key));
45+
const v = this.get(key);
46+
if (v !== undefined) {
47+
return v;
4548
}
46-
// biome-ignore lint/style/noNonNullAssertion: inserted before
47-
return this.get(key)!;
49+
50+
const defaultValue = callbackFunction(key);
51+
this.set(key, defaultValue);
52+
return defaultValue;
4853
};
4954
}

0 commit comments

Comments
 (0)