Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created August 23, 2022 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/f332fe03245ec64b1a85d571bbe0b887 to your computer and use it in GitHub Desktop.
Save peterbe/f332fe03245ec64b1a85d571bbe0b887 to your computer and use it in GitHub Desktop.
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