From 963d12e614707f1e6b7be518966ba99cecdbf20a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 Mar 2020 11:51:15 +0100 Subject: [PATCH] PadManager: use a set instead of an array in padlist Avoid looping on the array, especially useful if you have many pads. --HG-- branch : padlist-use-set --- src/node/db/PadManager.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/node/db/PadManager.js b/src/node/db/PadManager.js index 80283b1e8..89e6a84ab 100644 --- a/src/node/db/PadManager.js +++ b/src/node/db/PadManager.js @@ -49,8 +49,8 @@ var globalPads = { * Updated without db access as new pads are created/old ones removed. */ let padList = { - list: [], - sorted : false, + list: new Set(), + cachedList: undefined, initiated: false, init: async function() { let dbData = await db.findKeys("pad:*", "*:*:*"); @@ -78,29 +78,26 @@ let padList = { getPads: async function() { await this.load(); - if (!this.sorted) { - this.list.sort(); - this.sorted = true; + if (!this.cachedList) { + this.cachedList = Array.from(this.list).sort(); } - return this.list; + return this.cachedList; }, addPad: function(name) { if (!this.initiated) return; - if (this.list.indexOf(name) == -1) { - this.list.push(name); - this.sorted = false; + if (!this.list.has(name)) { + this.list.add(name); + this.cachedList = undefined; } }, removePad: function(name) { if (!this.initiated) return; - var index = this.list.indexOf(name); - - if (index > -1) { - this.list.splice(index, 1); - this.sorted = false; + if (this.list.has(name)) { + this.list.delete(name); + this.cachedList = undefined; } } };