speedtest
speedtest copied to clipboard
Bun Support Lacking - performance.clearResourceTimings not supported
Everything is 0 and this has no polyfill to a much better than node.js runtime gainign popularity
performance.clearResourceTimings is not in Bun
export async function testDownloadSpeed({
testSizes = [100000, 500000, 1000000],
iterations = 3,
decimalPrecision = 1,
CLOUDFLARE_URL = 'https://speed.cloudflare.com/__down'
} = {}) {
let total = 0, count = 0;
for (const size of testSizes) {
for (let i = 0; i < iterations; i++) {
try {
const t0 = Date.now();
const res = await fetch(`${CLOUDFLARE_URL}?bytes=${size}`);
if (!res.ok) continue;
await res.text();
const seconds = (Date.now() - t0) / 1000;
if (seconds > 0) {
total += size / (1024 * 1024) / seconds;
count++;
}
} catch {}
}
}
const avg = count ? total / count : 0;
return +avg.toFixed(decimalPrecision);
}
if (require.main === module) {
testDownloadSpeed().then(console.log);
}
The core function in a single simple call to the API. no need to make it s complex that it breaks bun compatibiltiy