Skip to content

Commit 7a072e0

Browse files
committed
polyfill: small optimizations
1 parent ae5ea98 commit 7a072e0

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

polyfill/collection-map.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { apply, mapSet, mapGet, mapHas, mapDelete, mapClear, freeze, mapSize } from "./internal/originals.ts";
22
import { isComposite } from "./composite.ts";
33
import { resolveKey, missing, clearCompMap, deleteKey } from "./internal/key-lookup.ts";
4+
import { EMPTY } from "./internal/utils.ts";
45

56
function requireInternalSlot(that: unknown): void {
6-
apply(mapSize, that, []);
7+
apply(mapSize, that, EMPTY);
78
}
89

910
export function mapPrototypeSet<K, V>(this: Map<K, V>, key: K, value: V): globalThis.Map<K, V> {
@@ -41,7 +42,7 @@ export function mapPrototypeGet<K, V>(this: Map<K, V>, key: K): any {
4142
}
4243

4344
export function mapPrototypeClear(this: Map<any, any>): void {
44-
apply(mapClear, this, []);
45+
apply(mapClear, this, EMPTY);
4546
clearCompMap(this);
4647
}
4748

polyfill/collection-set.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import {
1919
} from "./internal/originals.ts";
2020
import { isComposite } from "./composite.ts";
2121
import { resolveKey, missing, clearCompMap, deleteKey } from "./internal/key-lookup.ts";
22+
import { EMPTY } from "./internal/utils.ts";
2223

2324
function requireInternalSlot(that: unknown): void {
24-
apply(setSize, that, []);
25+
apply(setSize, that, EMPTY);
2526
}
2627

2728
function setPrototypeAdd<T>(this: Set<T>, value: T): Set<T> {
@@ -33,7 +34,7 @@ function setPrototypeAdd<T>(this: Set<T>, value: T): Set<T> {
3334

3435
function setPrototypeClear(this: Set<any>): void {
3536
requireInternalSlot(this);
36-
apply(setClear, this, []);
37+
apply(setClear, this, EMPTY);
3738
clearCompMap(this);
3839
}
3940

@@ -76,7 +77,7 @@ function setPrototypeIntersection<T, U>(this: Set<T>, other: ReadonlySetLike<U>)
7677
requireInternalSlot(this);
7778
const otherSet = getSetRecord(other);
7879
const result = new Set<any>();
79-
if (apply(setSize, this, []) <= otherSet.size) {
80+
if (apply(setSize, this, EMPTY) <= otherSet.size) {
8081
for (const value of setIterator(this)) {
8182
if (otherSet.has(value)) {
8283
apply(setPrototypeAdd, result, [value]);
@@ -133,7 +134,7 @@ function setPrototypeSymmetricDifference<T, U>(this: Set<T>, other: ReadonlySetL
133134
function setPrototypeIsSubsetOf<T, U>(this: Set<T>, other: ReadonlySetLike<U>): boolean {
134135
requireInternalSlot(this);
135136
const otherSet = getSetRecord(other);
136-
if (apply(setSize, this, []) > otherSet.size) return false;
137+
if (apply(setSize, this, EMPTY) > otherSet.size) return false;
137138
for (const value of setIterator(this)) {
138139
if (!otherSet.has(value)) {
139140
return false;
@@ -145,7 +146,7 @@ function setPrototypeIsSubsetOf<T, U>(this: Set<T>, other: ReadonlySetLike<U>):
145146
function setPrototypeIsSupersetOf<T, U>(this: Set<T>, other: ReadonlySetLike<U>): boolean {
146147
requireInternalSlot(this);
147148
const otherSet = getSetRecord(other);
148-
if (apply(setSize, this, []) < otherSet.size) return false;
149+
if (apply(setSize, this, EMPTY) < otherSet.size) return false;
149150
for (const value of otherSet.keys()) {
150151
if (!apply(setPrototypeHas, this, [value])) {
151152
return false;
@@ -158,7 +159,7 @@ function setPrototypeIsDisjointFrom<T, U>(this: Set<T>, other: ReadonlySetLike<U
158159
requireInternalSlot(this);
159160
const otherSet = getSetRecord(other);
160161

161-
if (apply(setSize, this, []) <= otherSet.size) {
162+
if (apply(setSize, this, EMPTY) <= otherSet.size) {
162163
for (const value of setIterator(this)) {
163164
if (otherSet.has(value)) {
164165
return false;
@@ -183,7 +184,7 @@ const setIteratorProto = {
183184
return this;
184185
},
185186
next() {
186-
return apply(this.nextFn, this.it, []);
187+
return apply(this.nextFn, this.it, EMPTY);
187188
},
188189
return(value: any) {
189190
const ret = this.it.return;
@@ -198,7 +199,7 @@ const setIteratorProto = {
198199
};
199200

200201
function setIterator<T>(set: Set<T>): Iterable<T> {
201-
const it = apply(setValues, set, []);
202+
const it = apply(setValues, set, EMPTY);
202203
return {
203204
__proto__: setIteratorProto,
204205
nextFn: setNext,
@@ -244,7 +245,7 @@ function getSetRecord(other: ReadonlySetLike<unknown>) {
244245
return Boolean(apply(has, other, [v]));
245246
},
246247
keys(): Iterable<any> {
247-
const it = apply(keys, other, []);
248+
const it = apply(keys, other, EMPTY);
248249
if (it === null || typeof it !== "object") {
249250
throw new TypeError("invalid keys");
250251
}

polyfill/internal/hash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function hashComposite(input: Composite): number {
4242
return cachedHash;
4343
}
4444
const hasher = new MurmurHashStream();
45-
const keys = apply(ownKeys, null, [input]);
45+
const keys = ownKeys(input);
4646
apply(sort, keys, [keySort]);
4747
for (let i = 0; i < keys.length; i++) {
4848
const key = keys[i];

polyfill/internal/hashmap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Map, mapSet, mapGet, apply, splice, mapDelete, mapClear, freeze } from "./originals.ts";
2+
import { EMPTY } from "./utils.ts";
23

34
const missing = Symbol("missing");
45

@@ -11,7 +12,7 @@ export class HashMap<K, V> {
1112
this.#equals = equals;
1213
}
1314
clear(): void {
14-
apply(mapClear, this.#map, []);
15+
apply(mapClear, this.#map, EMPTY);
1516
}
1617
#get(key: K): V | typeof missing {
1718
const hash = this.#hasher(key);

polyfill/internal/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { is } from "./originals.ts";
1+
import { is, freeze } from "./originals.ts";
22

33
export function assert(v: unknown): asserts v {
44
if (!v) {
@@ -13,3 +13,5 @@ export function assert(v: unknown): asserts v {
1313
export function sameValueZero(a: unknown, b: unknown): boolean {
1414
return is(a === 0 ? 0 : a, b === 0 ? 0 : b);
1515
}
16+
17+
export const EMPTY = freeze([]);

0 commit comments

Comments
 (0)