Created
August 23, 2022 19:38
-
-
Save peterbe/f332fe03245ec64b1a85d571bbe0b887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import got from 'got' | |
main() | |
async function main() { | |
const URLS = [ | |
'https://www.peterbe.com', | |
'https://docs.github.com/en', | |
'https://developer.mozilla.org', | |
'https://primer.style/', | |
] | |
console.time() | |
// const sizes = await getSizes(URLS) | |
const sizes = await getSizes_async(URLS) | |
console.log( | |
'SIZES', | |
sizes, | |
'TOTAL', | |
sizes.reduce((a, b) => a + b, 0) | |
) | |
console.timeEnd() | |
} | |
async function getSizes(urls) { | |
const sizes = [] | |
for (const url of urls) { | |
sizes.push(await getSize(url)) | |
} | |
return sizes | |
} | |
async function getSize(url) { | |
const r = await got(url) | |
console.log('FINISHED', url) | |
return r.body.length | |
} | |
async function getSizes_async(urls) { | |
const promises = [] | |
for (const url of urls) { | |
promises.push(getSize(url)) | |
} | |
return await Promise.all(promises) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment