From 051124f73e12df7a30c1a26e8f872a75705fb7ce Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 17:44:19 +0100 Subject: [PATCH 01/13] perf: `merge()`: deduplicate boolean checks --- packages/collection/src/collection.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 19fc01da9b11..5b060c6f6f45 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -959,12 +959,14 @@ export class Collection extends Map { const hasInSelf = this.has(key); const hasInOther = other.has(key); - if (hasInSelf && hasInOther) { - const result = whenInBoth(this.get(key)!, other.get(key)!, key); - if (result.keep) coll.set(key, result.value); - } else if (hasInSelf) { - const result = whenInSelf(this.get(key)!, key); - if (result.keep) coll.set(key, result.value); + if (hasInSelf) { + if (hasInOther) { + const result = whenInBoth(this.get(key)!, other.get(key)!, key); + if (result.keep) coll.set(key, result.value); + } else { + const result = whenInSelf(this.get(key)!, key); + if (result.keep) coll.set(key, result.value); + } } else if (hasInOther) { const result = whenInOther(other.get(key)!, key); if (result.keep) coll.set(key, result.value); From 96daba881f04062fb4e50021db7eccdf8cddd444 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 17:47:29 +0100 Subject: [PATCH 02/13] perf: `toSorted()`: remove redundant closure --- packages/collection/src/collection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 5b060c6f6f45..a18a977b4426 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -997,8 +997,8 @@ export class Collection extends Map { * collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp); * ``` */ - public toSorted(compareFunction: Comparator = Collection.defaultSort) { - return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk)); + public toSorted(compareFunction: Comparator = Collection.defaultSort): Collection { + return new this.constructor[Symbol.species](this).sort(compareFunction); } public toJSON() { From a9911d2412688914a1b7f1f018008cbe2686018e Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 17:48:24 +0100 Subject: [PATCH 03/13] perf: `last[Key]()`: order of operations - do not perform iterable-to-array until required - test ! before < --- packages/collection/src/collection.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index a18a977b4426..857dddd20e39 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -117,11 +117,14 @@ export class Collection extends Map { public last(): Value | undefined; public last(amount: number): Value[]; public last(amount?: number): Value | Value[] | undefined { - const arr = [...this.values()]; - if (amount === undefined) return arr[arr.length - 1]; - if (amount < 0) return this.first(amount * -1); + if (amount === undefined) { + const arr = [...this.values()]; + return arr[arr.length - 1]; + } if (!amount) return []; - return arr.slice(-amount); + if (amount < 0) return this.first(amount * -1); + const arr = [...this.values()]; + return arr.slice(amount * -1); } /** @@ -134,11 +137,14 @@ export class Collection extends Map { public lastKey(): Key | undefined; public lastKey(amount: number): Key[]; public lastKey(amount?: number): Key | Key[] | undefined { - const arr = [...this.keys()]; - if (amount === undefined) return arr[arr.length - 1]; - if (amount < 0) return this.firstKey(amount * -1); + if (amount === undefined) { + const arr = [...this.keys()]; + return arr[arr.length - 1]; + } if (!amount) return []; - return arr.slice(-amount); + if (amount < 0) return this.firstKey(amount * -1); + const arr = [...this.keys()]; + return arr.slice(amount * -1); } /** From 0a78efb4a227bf25008f37c071927511ff7cdd71 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 17:57:37 +0100 Subject: [PATCH 04/13] perf: `{at,keyAt}()`: manually iterate to target --- packages/collection/src/collection.ts | 34 ++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 857dddd20e39..9781bc878d42 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -154,10 +154,19 @@ export class Collection extends Map { * * @param index - The index of the element to obtain */ - public at(index: number) { - index = Math.floor(index); - const arr = [...this.values()]; - return arr.at(index); + public at(index: number): Value | undefined { + index = Math.trunc(index); + if (index >= 0) { + if (index >= this.size) return undefined; + } else { + if (index < this.size * -1) return undefined; + index += this.size; + } + const iter = this.values(); + for (let skip = 0; skip < index; skip++) { + iter.next(); + } + return iter.next().value!; } /** @@ -167,10 +176,19 @@ export class Collection extends Map { * * @param index - The index of the key to obtain */ - public keyAt(index: number) { - index = Math.floor(index); - const arr = [...this.keys()]; - return arr.at(index); + public keyAt(index: number): Key | undefined { + index = Math.trunc(index); + if (index >= 0) { + if (index >= this.size) return undefined; + } else { + if (index < this.size * -1) return undefined; + index += this.size; + } + const iter = this.keys(); + for (let skip = 0; skip < index; skip++) { + iter.next(); + } + return iter.next().value!; } /** From 09b50b580d44004b86dd0e2374693c2f8fb65160 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 18:57:29 +0100 Subject: [PATCH 05/13] perf: `first[Key]()`: avoid `Array.from()` --- packages/collection/src/collection.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 9781bc878d42..e2ae0785962b 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -87,7 +87,13 @@ export class Collection extends Map { if (amount < 0) return this.last(amount * -1); amount = Math.min(this.size, amount); const iter = this.values(); - return Array.from({ length: amount }, (): Value => iter.next().value!); + // eslint-disable-next-line unicorn/no-new-array + const results: Value[] = new Array(amount); + for (let index = 0; index < amount; index++) { + results[index] = iter.next().value!; + } + + return results; } /** @@ -104,7 +110,13 @@ export class Collection extends Map { if (amount < 0) return this.lastKey(amount * -1); amount = Math.min(this.size, amount); const iter = this.keys(); - return Array.from({ length: amount }, (): Key => iter.next().value!); + // eslint-disable-next-line unicorn/no-new-array + const results: Key[] = new Array(amount); + for (let index = 0; index < amount; index++) { + results[index] = iter.next().value!; + } + + return results; } /** @@ -121,6 +133,7 @@ export class Collection extends Map { const arr = [...this.values()]; return arr[arr.length - 1]; } + if (!amount) return []; if (amount < 0) return this.first(amount * -1); const arr = [...this.values()]; @@ -141,6 +154,7 @@ export class Collection extends Map { const arr = [...this.keys()]; return arr[arr.length - 1]; } + if (!amount) return []; if (amount < 0) return this.firstKey(amount * -1); const arr = [...this.keys()]; @@ -162,10 +176,12 @@ export class Collection extends Map { if (index < this.size * -1) return undefined; index += this.size; } + const iter = this.values(); for (let skip = 0; skip < index; skip++) { iter.next(); } + return iter.next().value!; } @@ -184,10 +200,12 @@ export class Collection extends Map { if (index < this.size * -1) return undefined; index += this.size; } + const iter = this.keys(); for (let skip = 0; skip < index; skip++) { iter.next(); } + return iter.next().value!; } From eb5c53a1fc7f3e0b65728db1d02b0f002a1b9a69 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 19:03:58 +0100 Subject: [PATCH 06/13] perf: `map()`: avoid `Array.from()` --- packages/collection/src/collection.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index e2ae0785962b..b9186414ddc8 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -553,10 +553,14 @@ export class Collection extends Map { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (thisArg !== undefined) fn = fn.bind(thisArg); const iter = this.entries(); - return Array.from({ length: this.size }, (): NewValue => { + // eslint-disable-next-line unicorn/no-new-array + const results: NewValue[] = new Array(this.size); + for (let index = 0; index < this.size; index++) { const [key, value] = iter.next().value!; - return fn(value, key, this); - }); + results[index] = fn(value, key, this); + } + + return results; } /** From ba0e0bee4e537d777510f452ef340d2c973b8514 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 20:18:15 +0100 Subject: [PATCH 07/13] perf: `random[Key]()`: avoid `Array.from()` --- packages/collection/src/collection.ts | 28 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index b9186414ddc8..19b14ddcee53 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -218,13 +218,17 @@ export class Collection extends Map { public random(): Value | undefined; public random(amount: number): Value[]; public random(amount?: number): Value | Value[] | undefined { + if (amount === 0) return []; const arr = [...this.values()]; if (amount === undefined) return arr[Math.floor(Math.random() * arr.length)]; - if (!arr.length || !amount) return []; - return Array.from( - { length: Math.min(amount, arr.length) }, - (): Value => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]!, - ); + amount = Math.min(this.size, amount); + // eslint-disable-next-line unicorn/no-new-array + const results: Value[] = new Array(amount); + for (let index = 0; index < amount; index++) { + results[index] = arr.splice(Math.floor(Math.random() * arr.length), 1)[0]!; + } + + return results; } /** @@ -236,13 +240,17 @@ export class Collection extends Map { public randomKey(): Key | undefined; public randomKey(amount: number): Key[]; public randomKey(amount?: number): Key | Key[] | undefined { + if (amount === 0) return []; const arr = [...this.keys()]; if (amount === undefined) return arr[Math.floor(Math.random() * arr.length)]; - if (!arr.length || !amount) return []; - return Array.from( - { length: Math.min(amount, arr.length) }, - (): Key => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]!, - ); + amount = Math.min(this.size, amount); + // eslint-disable-next-line unicorn/no-new-array + const results: Key[] = new Array(amount); + for (let index = 0; index < amount; index++) { + results[index] = arr.splice(Math.floor(Math.random() * arr.length), 1)[0]!; + } + + return results; } /** From 2b4201ca449ea96016989971f1e2e38094b1d543 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 21:33:14 +0100 Subject: [PATCH 08/13] test: `.{at,keyAt}()` indices --- .../collection/__tests__/collection.test.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/collection/__tests__/collection.test.ts b/packages/collection/__tests__/collection.test.ts index 0ea401fd44e2..ea8f918bdce6 100644 --- a/packages/collection/__tests__/collection.test.ts +++ b/packages/collection/__tests__/collection.test.ts @@ -70,12 +70,20 @@ describe('at() tests', () => { expect(coll.at(0)).toStrictEqual(1); }); + test('positive non-integer index', () => { + expect(coll.at(1.5)).toStrictEqual(2); + }); + test('negative index', () => { expect(coll.at(-1)).toStrictEqual(3); }); + test('negative non-integer index', () => { + expect(coll.at(-2.5)).toStrictEqual(2); + }); + test('invalid positive index', () => { - expect(coll.at(4)).toBeUndefined(); + expect(coll.at(3)).toBeUndefined(); }); test('invalid negative index', () => { @@ -432,12 +440,20 @@ describe('keyAt() tests', () => { expect(coll.keyAt(0)).toStrictEqual('a'); }); + test('positive non-integer index', () => { + expect(coll.keyAt(1.5)).toStrictEqual('b'); + }); + test('negative index', () => { expect(coll.keyAt(-1)).toStrictEqual('c'); }); + test('negative non-integer index', () => { + expect(coll.keyAt(-2.5)).toStrictEqual('b'); + }); + test('invalid positive index', () => { - expect(coll.keyAt(4)).toBeUndefined(); + expect(coll.keyAt(3)).toBeUndefined(); }); test('invalid negative index', () => { From dfa75d8eee5d3eda59cccd3495d57b8799e176af Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Fri, 11 Oct 2024 22:37:38 +0100 Subject: [PATCH 09/13] perf: `last[Key]()`: use `.at()`/`.keyAt()` for single element --- packages/collection/src/collection.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 19b14ddcee53..c199ffb15da6 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -129,13 +129,10 @@ export class Collection extends Map { public last(): Value | undefined; public last(amount: number): Value[]; public last(amount?: number): Value | Value[] | undefined { - if (amount === undefined) { - const arr = [...this.values()]; - return arr[arr.length - 1]; - } - + if (amount === undefined) return this.at(-1); if (!amount) return []; if (amount < 0) return this.first(amount * -1); + const arr = [...this.values()]; return arr.slice(amount * -1); } @@ -150,13 +147,10 @@ export class Collection extends Map { public lastKey(): Key | undefined; public lastKey(amount: number): Key[]; public lastKey(amount?: number): Key | Key[] | undefined { - if (amount === undefined) { - const arr = [...this.keys()]; - return arr[arr.length - 1]; - } - + if (amount === undefined) return this.keyAt(-1); if (!amount) return []; if (amount < 0) return this.firstKey(amount * -1); + const arr = [...this.keys()]; return arr.slice(amount * -1); } From 322c562c9be81c779f12c3599d554cbf3d315e15 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Sun, 13 Oct 2024 18:42:58 +0100 Subject: [PATCH 10/13] perf: `first[Key]()`: use iterable-to-array if returning all --- packages/collection/src/collection.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index c199ffb15da6..bb452c26cc25 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -85,7 +85,8 @@ export class Collection extends Map { public first(amount?: number): Value | Value[] | undefined { if (amount === undefined) return this.values().next().value; if (amount < 0) return this.last(amount * -1); - amount = Math.min(this.size, amount); + if (amount >= this.size) return [...this.values()]; + const iter = this.values(); // eslint-disable-next-line unicorn/no-new-array const results: Value[] = new Array(amount); @@ -108,7 +109,8 @@ export class Collection extends Map { public firstKey(amount?: number): Key | Key[] | undefined { if (amount === undefined) return this.keys().next().value; if (amount < 0) return this.lastKey(amount * -1); - amount = Math.min(this.size, amount); + if (amount >= this.size) return [...this.keys()]; + const iter = this.keys(); // eslint-disable-next-line unicorn/no-new-array const results: Key[] = new Array(amount); From 043c24eb3931c4330480a51e2e349df427c168d9 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Sun, 13 Oct 2024 18:49:04 +0100 Subject: [PATCH 11/13] perf: `random[Key]()`: use `{at,keyAt}()` for single value - skip iterable-to-array for returning single value - short-circuit if amount or collection size is zero --- packages/collection/src/collection.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index bb452c26cc25..f348282fd7b5 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -214,14 +214,15 @@ export class Collection extends Map { public random(): Value | undefined; public random(amount: number): Value[]; public random(amount?: number): Value | Value[] | undefined { - if (amount === 0) return []; - const arr = [...this.values()]; - if (amount === undefined) return arr[Math.floor(Math.random() * arr.length)]; + if (amount === undefined) return this.at(Math.floor(Math.random() * this.size)); amount = Math.min(this.size, amount); + if (!amount) return []; + + const values = [...this.values()]; // eslint-disable-next-line unicorn/no-new-array const results: Value[] = new Array(amount); for (let index = 0; index < amount; index++) { - results[index] = arr.splice(Math.floor(Math.random() * arr.length), 1)[0]!; + results[index] = values.splice(Math.floor(Math.random() * values.length), 1)[0]!; } return results; @@ -236,14 +237,15 @@ export class Collection extends Map { public randomKey(): Key | undefined; public randomKey(amount: number): Key[]; public randomKey(amount?: number): Key | Key[] | undefined { - if (amount === 0) return []; - const arr = [...this.keys()]; - if (amount === undefined) return arr[Math.floor(Math.random() * arr.length)]; + if (amount === undefined) return this.keyAt(Math.floor(Math.random() * this.size)); amount = Math.min(this.size, amount); + if (!amount) return []; + + const keys = [...this.keys()]; // eslint-disable-next-line unicorn/no-new-array const results: Key[] = new Array(amount); for (let index = 0; index < amount; index++) { - results[index] = arr.splice(Math.floor(Math.random() * arr.length), 1)[0]!; + results[index] = keys.splice(Math.floor(Math.random() * keys.length), 1)[0]!; } return results; From a998088fcc1d58ba3ba41ce17a7e0f58b6960a82 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Mon, 4 Nov 2024 19:33:04 +0100 Subject: [PATCH 12/13] perf: `random[Key]()`: use Durstenfeld shuffle --- packages/collection/src/collection.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index f348282fd7b5..4edb3ca9d2f6 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -219,13 +219,12 @@ export class Collection extends Map { if (!amount) return []; const values = [...this.values()]; - // eslint-disable-next-line unicorn/no-new-array - const results: Value[] = new Array(amount); - for (let index = 0; index < amount; index++) { - results[index] = values.splice(Math.floor(Math.random() * values.length), 1)[0]!; + for (let sourceIndex = 0; sourceIndex < amount; sourceIndex++) { + const targetIndex = sourceIndex + Math.floor(Math.random() * (values.length - sourceIndex)); + [values[sourceIndex], values[targetIndex]] = [values[targetIndex]!, values[sourceIndex]!]; } - return results; + return values.slice(0, amount); } /** @@ -242,13 +241,12 @@ export class Collection extends Map { if (!amount) return []; const keys = [...this.keys()]; - // eslint-disable-next-line unicorn/no-new-array - const results: Key[] = new Array(amount); - for (let index = 0; index < amount; index++) { - results[index] = keys.splice(Math.floor(Math.random() * keys.length), 1)[0]!; + for (let sourceIndex = 0; sourceIndex < amount; sourceIndex++) { + const targetIndex = sourceIndex + Math.floor(Math.random() * (keys.length - sourceIndex)); + [keys[sourceIndex], keys[targetIndex]] = [keys[targetIndex]!, keys[sourceIndex]!]; } - return results; + return keys.slice(0, amount); } /** From fa9fca3bf7ab97ec1ee6ae6d224ecf699eb0b907 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Mon, 4 Nov 2024 19:36:02 +0100 Subject: [PATCH 13/13] refactor: `{key,keyAt}()`: reorder index check --- packages/collection/src/collection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 4edb3ca9d2f6..5fa11cb1ebb9 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -169,8 +169,8 @@ export class Collection extends Map { if (index >= 0) { if (index >= this.size) return undefined; } else { - if (index < this.size * -1) return undefined; index += this.size; + if (index < 0) return undefined; } const iter = this.values(); @@ -193,8 +193,8 @@ export class Collection extends Map { if (index >= 0) { if (index >= this.size) return undefined; } else { - if (index < this.size * -1) return undefined; index += this.size; + if (index < 0) return undefined; } const iter = this.keys();