2 ms·
There's an easy but terrible fix for event loops lag. By app means, limit your work, do hard stuff like these folks did. But. If you just want to stop the suffe
by jauntywundrkind 2y ago
There's an easy but terrible fix for event loops lag. By app means, limit your work, do hard stuff like these folks did. But. If you just want to stop the suffering, all you need to do is yield periodically!
if (n % 1000 === 0) await require('node:timers/promises').setImmediste()
If you sleep in your async functions, other work can flow. Just unblock the other work.
Node calls this partitioning your work. https://nodejs.org/en/learn/asynchronous-work/dont-block-the-event-loop#partitioning https://nodejs.org/en/learn/asynchronous-work/dont-block-the...
Maybe don't use this specific library but it's pretty easy to rebuild everyday array iterators (forEach, reduce, map) to automatically yield every n iterations. https://www.npmjs.com/package/nice-loops https://www.npmjs.com/package/nice-loops