Skip to content

Instantly share code, notes, and snippets.

@cajames
Last active September 28, 2017 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cajames/ea919e321c061c4c1245f6eed952eafa to your computer and use it in GitHub Desktop.
Save cajames/ea919e321c061c4c1245f6eed952eafa to your computer and use it in GitHub Desktop.
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