Quick test to understand effect of mass amount of parallel promises in Node
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
async function test(max) { | |
const promises = []; | |
if (max < 1000) | |
throw new Error('max should be greater than 1000'); | |
for (let a = 0; a <= max; a++) { | |
if (a === 0) | |
console.time(`time-${max}-first`); | |
if (a === 500) | |
console.time(`time-${max}-500`); | |
if (a === max) | |
console.time(`time-${max}-last`); | |
promises.push(promA(a, max)); | |
} | |
await Promise.all(promises); | |
} | |
function promA(index, max) { | |
return new Promise((resolve) => { | |
console.info('started working on: ' + index); | |
setTimeout(() => { | |
if (index === 0) | |
console.timeEnd(`time-${max}-first`); | |
if (index === 500) | |
console.timeEnd(`time-${max}-500`); | |
if (index === max) | |
console.timeEnd(`time-${max}-last`); | |
console.info('actually resolved: ' + index); | |
resolve(1); | |
}, 0); | |
}); | |
} | |
async function run() { | |
await test(1000); | |
await test(10000); | |
await test(100000); | |
await test(1000000); | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment