Created
April 12, 2022 21:57
-
-
Save jamesseanwright/0bd92a473f07cf3cff27c8ac070fed94 to your computer and use it in GitHub Desktop.
Benchmarking serial vs parallel Promise executions
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
'use strict'; | |
const http = require('http'); | |
const getQuote = () => | |
new Promise((resolve, reject) => { | |
http.get('http://ron-swanson-quotes.herokuapp.com/v2/quotes', res => { | |
res.setEncoding('utf8'); | |
let data = ''; | |
res.on('data', chunk => { | |
data += chunk; | |
}); | |
res.on('end', () => { | |
resolve(data); | |
}); | |
res.on('error', e => { | |
reject(e); | |
}); | |
}); | |
}); | |
(async () => { | |
console.time('Serial requests'); | |
await getQuote(); | |
await getQuote(); | |
await getQuote(); | |
await getQuote(); | |
await getQuote(); | |
console.timeEnd('Serial requests'); | |
console.time('Parallel requests'); | |
await Promise.all([ | |
getQuote(), | |
getQuote(), | |
getQuote(), | |
getQuote(), | |
getQuote(), | |
]) | |
console.timeEnd('Parallel requests'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment