Skip to content

Commit 7165924

Browse files
⚡ Optimize measureTcpLatency to run in parallel (#48)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 2be3698 commit 7165924

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

src/utils/runtime.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,11 @@ export async function measureTcpLatency(
9898
} = {},
9999
): Promise<number> {
100100
const { host, port } = resolveTcpTarget(input);
101-
const latencies: number[] = [];
102-
103-
for (let i = 0; i < attempts; i++) {
104-
const latency = await measureTcpConnectOnce(host, port, timeout);
105-
if (Number.isFinite(latency)) {
106-
latencies.push(latency);
107-
}
108-
}
101+
const promises = Array.from({ length: attempts }, () =>
102+
measureTcpConnectOnce(host, port, timeout),
103+
);
104+
const results = await Promise.all(promises);
105+
const latencies = results.filter(Number.isFinite);
109106

110107
if (latencies.length === 0) {
111108
return Number.POSITIVE_INFINITY;

0 commit comments

Comments
 (0)