From 5cd7ce2efaf4a7dbbf27272f00e8d3d810e22500 Mon Sep 17 00:00:00 2001 From: valer23 Date: Fri, 17 Apr 2026 10:42:37 +0200 Subject: [PATCH] =?UTF-8?q?perf:=20avoid=20O(n=C2=B2)=20indexOf=20lookup?= =?UTF-8?q?=20in=20classic=20UI=20server=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Build an array of {idx, server} pairs before sorting so the original SPEEDTEST_SERVERS index is carried through, eliminating the per-option indexOf call. --- index-classic.html | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/index-classic.html b/index-classic.html index 036191c..b8f2d5c 100644 --- a/index-classic.html +++ b/index-classic.html @@ -69,18 +69,22 @@ country = country.replace(/\s*\([^)]*\)\s*/g, "").trim(); return { country: country, city: city }; } - var sortedServers = SPEEDTEST_SERVERS.slice().sort(function (a, b) { - var pa = parseServerName(a.name); - var pb = parseServerName(b.name); + var indexed = []; + for (var j = 0; j < SPEEDTEST_SERVERS.length; j++) { + indexed.push({ idx: j, server: SPEEDTEST_SERVERS[j] }); + } + indexed.sort(function (a, b) { + var pa = parseServerName(a.server.name); + var pb = parseServerName(b.server.name); return pa.country.localeCompare(pb.country) || pa.city.localeCompare(pb.city); }); //populate server list for manual selection - for (var i = 0; i < sortedServers.length; i++) { - if (sortedServers[i].pingT == -1) continue; + for (var i = 0; i < indexed.length; i++) { + if (indexed[i].server.pingT == -1) continue; var option = document.createElement("option"); - option.value = SPEEDTEST_SERVERS.indexOf(sortedServers[i]); - option.textContent = sortedServers[i].name; - if (sortedServers[i] === server) option.selected = true; + option.value = indexed[i].idx; + option.textContent = indexed[i].server.name; + if (indexed[i].server === server) option.selected = true; I("server").appendChild(option); } //show test UI