improve funkyArray's efficiency

This commit is contained in:
Holly Stubbs 2021-10-21 11:37:11 +01:00
parent 67a995fb5a
commit 3ece5a575d
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E

View file

@ -22,23 +22,26 @@ module.exports = class {
let id; let id;
if (this.indexingMode) { if (this.indexingMode) {
this.items[id = this.index] = item; this.items[id = this.index] = item;
this.index++;
} else { } else {
this.items[id = pRandom()] = item; this.items[id = pRandom()] = item;
} }
if (regenerate) this.regenerateIterableArray(); if (regenerate) {
this.regenerateIterableArray();
this.itemKeys = Object.keys(this.items); this.itemKeys = Object.keys(this.items);
}
this.index++;
return this.items[id]; return this.items[id];
} }
remove(id, regenerate = true) { remove(id, regenerate = true) {
delete this.items[id]; delete this.items[id];
if (regenerate) this.regenerateIterableArray(); if (regenerate) {
this.regenerateIterableArray();
this.itemKeys = Object.keys(this.items); this.itemKeys = Object.keys(this.items);
} }
}
removeFirstItem(regenerate = true) { removeFirstItem(regenerate = true) {
delete this.items[this.itemKeys[0]]; delete this.items[this.itemKeys[0]];
@ -51,6 +54,7 @@ module.exports = class {
for (let itemKey of this.itemKeys) { for (let itemKey of this.itemKeys) {
this.iterableArray.push(this.items[itemKey]); this.iterableArray.push(this.items[itemKey]);
} }
this.itemKeys = Object.keys(this.items);
} }
getFirstItem() { getFirstItem() {