From 9d3cc02ac5082937c62dd7714a9292f3600f1b03 Mon Sep 17 00:00:00 2001 From: Bruce Date: Sun, 8 Sep 2024 20:11:21 +0800 Subject: [PATCH] feat: reduce length of array. --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index bb73cb3..a7544ac 100644 --- a/index.js +++ b/index.js @@ -113,7 +113,7 @@ async function main() { console.log(`Current progress:`); terminalBarUI.start(ips.length, 0); - const unsortedArr = []; + let unsortedArr = []; for (let i = 0; i < ips.length; i++) { const ip = ips[i]; @@ -125,13 +125,19 @@ async function main() { unsortedArr.push({ ip, latency: avgLatency }); } countOfBeingProcess--; + if (unsortedArr.length > 150) { + unsortedArr = unsortedArr.sort((a, b) => { + return a.latency - b.latency; + }); + unsortedArr.slice(100, 150); + console.warn('removed 50 IPs.'); + } } else { countOfBeingProcess++; queryAvgLatency(ip).then(function (avgLatency) { if (avgLatency < THRESHOLD) { unsortedArr.push({ ip, latency: avgLatency }); - console.log(`Added ${ip} with latency ${avgLatency}`); } countOfBeingProcess--; }).catch(function (e) {