mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
perf(collection): optimisations (#10552)
* perf: `merge()`: deduplicate boolean checks
* perf: `toSorted()`: remove redundant closure
* perf: `last[Key]()`: order of operations
- do not perform iterable-to-array until required
- test ! before <
* perf: `{at,keyAt}()`: manually iterate to target
* perf: `first[Key]()`: avoid `Array.from()`
* perf: `map()`: avoid `Array.from()`
* perf: `random[Key]()`: avoid `Array.from()`
* test: `.{at,keyAt}()` indices
* perf: `last[Key]()`: use `.at()`/`.keyAt()` for single element
* perf: `first[Key]()`: use iterable-to-array if returning all
* 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
* perf: `random[Key]()`: use Durstenfeld shuffle
* refactor: `{key,keyAt}()`: reorder index check
---------
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user