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
1 changed files with 10 additions and 6 deletions

View File

@ -22,22 +22,25 @@ 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.itemKeys = Object.keys(this.items); this.regenerateIterableArray();
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.itemKeys = Object.keys(this.items); this.regenerateIterableArray();
this.itemKeys = Object.keys(this.items);
}
} }
removeFirstItem(regenerate = true) { removeFirstItem(regenerate = true) {
@ -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() {