mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Expand collection array caching
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -3,36 +3,48 @@
|
||||
* @extends {Map}
|
||||
*/
|
||||
class Collection extends Map {
|
||||
set(key, value) {
|
||||
super.set(key, value);
|
||||
this.changed = true;
|
||||
constructor(iterable) {
|
||||
super(iterable);
|
||||
this._array = null;
|
||||
this._keyArray = null;
|
||||
}
|
||||
|
||||
set(key, val) {
|
||||
super.set(key, val);
|
||||
this._array = null;
|
||||
this._keyArray = null;
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
super.delete(key);
|
||||
this.changed = true;
|
||||
this._array = null;
|
||||
this._keyArray = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ordered array of the values of this collection.
|
||||
* 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 add/remove elements on the array.
|
||||
* @returns {Array}
|
||||
* @example
|
||||
* // identical to:
|
||||
* Array.from(collection.values());
|
||||
*/
|
||||
array() {
|
||||
return Array.from(this.values());
|
||||
if (!this._array || this._array.length !== this.size) this._array = Array.from(this.values());
|
||||
return this._array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ordered array of the keys of this collection.
|
||||
* 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 add/remove elements on the array.
|
||||
* @returns {Array}
|
||||
* @example
|
||||
* // identical to:
|
||||
* Array.from(collection.keys());
|
||||
*/
|
||||
keyArray() {
|
||||
return Array.from(this.keys());
|
||||
if (!this._keyArray || this._keyArray.length !== this.size) this._keyArray = Array.from(this.keys());
|
||||
return this._keyArray;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,11 +89,8 @@ class Collection extends Map {
|
||||
* @returns {*}
|
||||
*/
|
||||
random() {
|
||||
if (!this.cachedArray || this.cachedArray.length !== this.size || this.changed) {
|
||||
this.cachedArray = this.array();
|
||||
this.changed = false;
|
||||
}
|
||||
return this.cachedArray[Math.floor(Math.random() * this.cachedArray.length)];
|
||||
const arr = this.array();
|
||||
return arr[Math.floor(Math.random() * arr.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,11 +99,8 @@ class Collection extends Map {
|
||||
* @returns {*}
|
||||
*/
|
||||
randomKey() {
|
||||
if (!this.cachedKeyArray || this.cachedKeyArray.length !== this.size || this.changed) {
|
||||
this.cachedKeyArray = this.keyArray();
|
||||
this.changed = false;
|
||||
}
|
||||
return this.cachedKeyArray[Math.floor(Math.random() * this.cachedKeyArray.length)];
|
||||
const arr = this.keyArray();
|
||||
return arr[Math.floor(Math.random() * arr.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user