2 ms·
Javascript is better, but its still sloppy: tasks.push( (async () => { await delay(10000); })() ); .. So tasks.push is called with
by josephg 3y ago
Javascript is better, but its still sloppy:
tasks.push(
(async () => {
await delay(10000);
})()
);
.. So tasks.push is called with an IIFE, which returns a promise, which resolves after its internally awaited another promise...
You could just do this, which avoids about 3 unnecessary allocations:
tasks.push(delay(10000))
And I'm not convinced promisify(setTimeout) is what you want here. I'd just use this:
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout))
... Which honestly should be part of the standard library in javascript. However, I'm less convinced that replacing the delay function will make a big difference to performance.
- pkolaczk 3y agoThanks for the suggestion. I fixed it and indeed it improved memory consumption of NodeJS.
- josephg 3y agoHeh confirming the old adage: If you want to know the right answer to a problem online, post the wrong answer. People will come out of the woodwork to correct you. I uh, may be in this picture.
- mhio 3y agoThe article updated to remove the cruft you noted. It dropped the 1M memory substantially Promise timers are now packaged by node at least, they allocate about 20% less memory for the 1mil test (on v18). const { setTimeout } = require('node:timers/promises') https://nodejs.org/api/timers.html#timers-promises-api https://nodejs.org/api/timers.html#timers-promises-api
- josephg 3y agoOh I didn't know that! Thanks!