Skip to content

Instantly share code, notes, and snippets.

@jamesseanwright
Created April 12, 2022 21:57
Show Gist options
  • Save jamesseanwright/0bd92a473f07cf3cff27c8ac070fed94 to your computer and use it in GitHub Desktop.
Save jamesseanwright/0bd92a473f07cf3cff27c8ac070fed94 to your computer and use it in GitHub Desktop.
Benchmarking serial vs parallel Promise executions
'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