user3180402
user3180402

Reputation: 599

Parallel execution in Node

I have the below code which check for each user in an array. If found, the switch case will work accordingly.

var users = ["A","B"];
function searchUser() {
for (var j = 0; j < users.length; j++) {
        execute(users[j]);
    }
}

function execute() {
switch (selectUser) {
            case "A":
            {
                return new A();
                break;
            }
            case "B":
            {
                return new B();
                break;
            }
            ............
            }
}

How can I make it run parallel. Now Im getting the output of A() and then B(). But I want it to happen vice versa also.

Upvotes: 0

Views: 180

Answers (2)

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

Though you cannot make them running truly in parallel, you can make it more asynchronous with the use of async module: https://github.com/caolan/async .

Upvotes: 1

aryann
aryann

Reputation: 949

This code looks CPU intensive and probably does not have IO involved. The node is single-threaded model so parallel processing cannot on a single core (for multiple cores you need to start multiple node processes). On the single core the throughput increases only if you have IO involved. If your present code does not have IO involved, you may not gain anything.

Upvotes: 0

Related Questions