Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active September 18, 2017 08:01
Show Gist options
  • Save myfreeer/8b57e8a7cf6169e656fb694ab91279ac to your computer and use it in GitHub Desktop.
Save myfreeer/8b57e8a7cf6169e656fb694ab91279ac to your computer and use it in GitHub Desktop.
let arr = `112.80.248.52
27.221.40.46
119.75.219.40
111.13.12.44
111.13.12.22
111.13.100.27
117.185.16.83
117.185.16.84
117.185.16.13`.split('\n');
arr = arr.map(url=>'http://'+url);
const connectSpeed = async(...urls) => {
if (urls.length === 1 && urls[0].push) urls = urls[0];
let result = {};
const initTime = performance.now();
await Promise.all(urls.map(url => fetch(url).then(res => res.text()).then(() => (result[url] = performance.now() - initTime)).catch(e => (result[url] = e))));
let speeds = [];
for (let i in result) speeds.push([i, result[i]]);
speeds.sort((a, b) => a[1] - b[1]);
return speeds.map(e=>e[0].replace(/^http:\/\//,''));
};
connectSpeed(arr).then(speeds=>console.log(speeds[0]))
const connectSpeed = async(...urls) => {
if (urls.length === 1 && urls[0].push) urls = urls[0];
let result = {};
const initTime = performance.now();
await Promise.all(urls.map(url => fetch(url).then(res => res.text()).then(() => (result[url] = performance.now() - initTime)).catch(e => (result[url] = e))));
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment