Skip to content

Commit 5cd7ce2

Browse files
valer23sstidl
authored andcommitted
perf: avoid O(n²) indexOf lookup in classic UI server loop
Build an array of {idx, server} pairs before sorting so the original SPEEDTEST_SERVERS index is carried through, eliminating the per-option indexOf call.
1 parent 69fa7a4 commit 5cd7ce2

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

index-classic.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,22 @@
6969
country = country.replace(/\s*\([^)]*\)\s*/g, "").trim();
7070
return { country: country, city: city };
7171
}
72-
var sortedServers = SPEEDTEST_SERVERS.slice().sort(function (a, b) {
73-
var pa = parseServerName(a.name);
74-
var pb = parseServerName(b.name);
72+
var indexed = [];
73+
for (var j = 0; j < SPEEDTEST_SERVERS.length; j++) {
74+
indexed.push({ idx: j, server: SPEEDTEST_SERVERS[j] });
75+
}
76+
indexed.sort(function (a, b) {
77+
var pa = parseServerName(a.server.name);
78+
var pb = parseServerName(b.server.name);
7579
return pa.country.localeCompare(pb.country) || pa.city.localeCompare(pb.city);
7680
});
7781
//populate server list for manual selection
78-
for (var i = 0; i < sortedServers.length; i++) {
79-
if (sortedServers[i].pingT == -1) continue;
82+
for (var i = 0; i < indexed.length; i++) {
83+
if (indexed[i].server.pingT == -1) continue;
8084
var option = document.createElement("option");
81-
option.value = SPEEDTEST_SERVERS.indexOf(sortedServers[i]);
82-
option.textContent = sortedServers[i].name;
83-
if (sortedServers[i] === server) option.selected = true;
85+
option.value = indexed[i].idx;
86+
option.textContent = indexed[i].server.name;
87+
if (indexed[i].server === server) option.selected = true;
8488
I("server").appendChild(option);
8589
}
8690
//show test UI

0 commit comments

Comments
 (0)