Last active
December 6, 2019 22:46
-
-
Save davidgilbertson/9d440d27da7f6c60a714fa027136580b 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
console.clear(); | |
(async () => { | |
const list = ['one', 'two', 'three']; | |
const doSlowly = msg => new Promise(resolve => { | |
setTimeout(() => { | |
resolve(`Processed ${msg}`); | |
}, 100); | |
}); | |
console.time('Processed sequentially in'); | |
const serialResults = []; | |
for (const listItem of list) { | |
const result = await doSlowly(listItem); | |
serialResults.push(result); | |
} | |
console.timeEnd('Processed sequentially in'); | |
console.log('Serial results:', serialResults); | |
console.time('Processed in parallel in'); | |
const parallelResults = await Promise.all(list.map(doSlowly)); | |
console.timeEnd('Processed in parallel in'); | |
console.log('Parallel results:', parallelResults); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment