From 3ece5a575de8cf731054caf33bfcff9c51838960 Mon Sep 17 00:00:00 2001 From: Holly Date: Thu, 21 Oct 2021 11:37:11 +0100 Subject: [PATCH] improve funkyArray's efficiency --- server/Util/funkyArray.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/server/Util/funkyArray.js b/server/Util/funkyArray.js index d7381c3..434ab89 100644 --- a/server/Util/funkyArray.js +++ b/server/Util/funkyArray.js @@ -22,22 +22,25 @@ module.exports = class { let id; if (this.indexingMode) { this.items[id = this.index] = item; + this.index++; } else { this.items[id = pRandom()] = item; } - if (regenerate) this.regenerateIterableArray(); - this.itemKeys = Object.keys(this.items); - - this.index++; + if (regenerate) { + this.regenerateIterableArray(); + this.itemKeys = Object.keys(this.items); + } return this.items[id]; } remove(id, regenerate = true) { delete this.items[id]; - if (regenerate) this.regenerateIterableArray(); - this.itemKeys = Object.keys(this.items); + if (regenerate) { + this.regenerateIterableArray(); + this.itemKeys = Object.keys(this.items); + } } removeFirstItem(regenerate = true) { @@ -51,6 +54,7 @@ module.exports = class { for (let itemKey of this.itemKeys) { this.iterableArray.push(this.items[itemKey]); } + this.itemKeys = Object.keys(this.items); } getFirstItem() {