Created
November 3, 2022 20:36
-
-
Save vitaly-t/72ef885b61ae5f543e2d858e38cf70de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
////////////////// | |
// RUN THIS FIRST: npm install [email protected] | |
// | |
// The example below crashes NodeJS (it runs out of memory) | |
// operator source: https://github.com/vitaly-t/iter-ops/blob/wait-cache/src/ops/async/wait-cache.ts | |
////////////////// | |
import {map, pipeAsync, waitCache} from 'iter-ops'; | |
function* gen(n) { | |
while (n > 0) { | |
yield n--; | |
} | |
} | |
const i = pipeAsync(gen(1_000_000), map(a => Promise.resolve(a)), waitCache(100)); | |
(async function () { | |
const start = Date.now(); | |
for await(const a of i) { | |
// console.log(a); | |
} | |
console.log('duration:', Date.now() - start); | |
})(); |
A bit more tests, and I'm beginning to see that Promise.race
is the culprit, it is godawful slow, and eats up all memory.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note it uses beta of
iter-ops
package:npm install [email protected]
, with the operator source here.If we reduce cache size to 10, for example, it works correctly, though not very performant. I cannot figure out what is so heavy in this algorithm that causes NodeJS to crash.