0

How to properly write the following set of loops in Node.Js without blocking the event loop or without causing out of memory error.

What I have tried so far includes

  • Combinations of setImmediate()/setInterval()
  • The Async module see my code here
  • Thread_a_gogo (this module is no more maintained)

The code.

for(var i = 0; i < 2000; i++)
    for(var j = 0; i < 2000; j++)
        for(var k = 0; k < 2000; k++)
            console.log(i + ":" + j + ":" + k);

Also created a JSFiddle to play around here

3
  • 1
    I don't think the event loop is what you want to use for computations. Its purpose is for handling async IO. Spinning up a new process that does for loops would be better. Commented Nov 29, 2016 at 16:09
  • You mean to suggest using child_process for example? Commented Nov 29, 2016 at 17:30
  • Your async.js code should work, you just have to actually use the asynchronous setImmediate(cb) (etc) instead of immediately calling cb(). Commented Nov 29, 2016 at 19:31

2 Answers 2

1

Not really sure what your use-case is. Javascript is blocking as it's single threaded, it would have to run the loop before moving to something else.

You could for example use a generator to run each iteration on an event though.

function* ticker() {
  for(let i = 0; i < 10; i++)
    for(let j = 0; i < 10; j++)
        for(let k = 0; k < 10; k++)
            yield[i, j, k];
}

const gen = ticker();

setInterval(() => console.log(gen.next().value), 500);

console.log('I am at the end (called straight away)');
Sign up to request clarification or add additional context in comments.

1 Comment

This never stops. You need to test for .next().done and when it's true, clear the interval.
1

You need to combine setImmediate/setTimeout/etc. with the async.js library. async.each is only for orchestration, it doesn't provide any asynchrony itself.

function getPerm(reducedWordList, callback) {
    var sevenLtrWords = _.filter(reducedWordList, word => word.length == 7); 
    var fourLtrWords = _.filter(reducedWordList, word => word.length == 4); 

    async.each(sevenLtrWords, (i, cb) => {
        async.each(sevenLtrWords, (j, cb) => {
            async.each(fourLtrWords, (k, cb) => {
                console.log(i + " " + j + " " + k); 
                setTimeout(cb, 0);
            }, cb);
        }, cb);
    }, callback);
}

2 Comments

If I understand this correctly, the setTimeOut() in the above code, will only let the innermost async to tick the event loop right?
It's setTimeout itself that ticks the event loop. All of these cbs just advance their respective iteration when called, and when the array end is met then it calls the next outer callback instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.