mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
perf(Collection): Performance improvements (#2342)
* Update Collection.js * ESLint
This commit is contained in:
@@ -39,22 +39,24 @@ class Collection extends Map {
|
|||||||
/**
|
/**
|
||||||
* Creates an ordered array of the values of this collection, and caches it internally. The array will only be
|
* Creates an ordered array of the values of this collection, and caches it internally. The array will only be
|
||||||
* reconstructed if an item is added to or removed from the collection, or if you change the length of the array
|
* reconstructed if an item is added to or removed from the collection, or if you change the length of the array
|
||||||
* itself. If you don't want this caching behaviour, use `Array.from(collection.values())` instead.
|
* itself. If you don't want this caching behaviour, use `[...collection.values()]` or
|
||||||
|
* `Array.from(collection.values())` instead.
|
||||||
* @returns {Array}
|
* @returns {Array}
|
||||||
*/
|
*/
|
||||||
array() {
|
array() {
|
||||||
if (!this._array || this._array.length !== this.size) this._array = Array.from(this.values());
|
if (!this._array || this._array.length !== this.size) this._array = [...this.values()];
|
||||||
return this._array;
|
return this._array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an ordered array of the keys of this collection, and caches it internally. The array will only be
|
* Creates an ordered array of the keys of this collection, and caches it internally. The array will only be
|
||||||
* reconstructed if an item is added to or removed from the collection, or if you change the length of the array
|
* reconstructed if an item is added to or removed from the collection, or if you change the length of the array
|
||||||
* itself. If you don't want this caching behaviour, use `Array.from(collection.keys())` instead.
|
* itself. If you don't want this caching behaviour, use `[...collection.keys()]` or
|
||||||
|
* `Array.from(collection.keys())` instead.
|
||||||
* @returns {Array}
|
* @returns {Array}
|
||||||
*/
|
*/
|
||||||
keyArray() {
|
keyArray() {
|
||||||
if (!this._keyArray || this._keyArray.length !== this.size) this._keyArray = Array.from(this.keys());
|
if (!this._keyArray || this._keyArray.length !== this.size) this._keyArray = [...this.keys()];
|
||||||
return this._keyArray;
|
return this._keyArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -421,7 +423,7 @@ class Collection extends Map {
|
|||||||
* @returns {Collection}
|
* @returns {Collection}
|
||||||
*/
|
*/
|
||||||
sort(compareFunction = (x, y) => +(x > y) || +(x === y) - 1) {
|
sort(compareFunction = (x, y) => +(x > y) || +(x === y) - 1) {
|
||||||
return new Collection(Array.from(this.entries()).sort((a, b) => compareFunction(a[1], b[1], a[0], b[0])));
|
return new Collection([...this.entries()].sort((a, b) => compareFunction(a[1], b[1], a[0], b[0])));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user