Skip to content

Instantly share code, notes, and snippets.

@cajames
Last active Sep 28, 2017
Embed
What would you like to do?
Quick test to understand effect of mass amount of parallel promises in Node
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