Is there a way to do node.js forEach in parallel?

Asked 2 years ago, Updated 2 years ago, 118 views

move on node.jsIf there is an array of objects in JavaScript code, would it be possible to move them in parallel when we rotate them in forEach?(Promise if possible)

 varobj=[{a:"abc", b:123, c:true}, {a:"efg", b:456, c:true}, {a:"hij", b:789, c:false}];
obj.forEach(function(data){
    // Process here
});

How do I write this data in parallel?

node.js

2022-09-30 20:48

2 Answers

However, if you want to delay and prioritize other processes,
I think you can use setTimeout.
I think overhead is going to be more expensive for something like the example.

Or if you really want to do it in parallel,
There's something like ParallelArray that Mozilla was working on.
You can reproduce it using ChildProcess and Promise, but
As long as you don't do much heavy work on data, overhead will still be more expensive.


2022-09-30 20:48

Note: February 26, 2016: I answered by confusing "parallel processing" and "asynchronous processing".The answers below are left as examples of "waiting for all processing to complete" and "waiting for the first processing to complete" for asynchronous processing that receives each element of obj.

Use Promise.all (perform each parallel perform each operation and wait for all operations to complete) or Promise.raceperform each operation and wait for the first one to complete.

index.js

 varobj=[
  {a:"abc", b:123, c:true},
  {a:"efg", b:456, c:true},
  {a:"hij", b:789, c:false}
];

varpromises=obj.map(function(data){
  return new Promise (function(resolve, reject) {
    // Process here
    var delay=Math.floor(Math.random()*5000); /* Randomly 0-5 seconds*/
    setTimeout(function(){
      console.log('processing:', delay+'[msec]', data);
      // Resolve() on completion (success) and reject() on failure
      resolve(data);
    }, delay);
  });
});

Promise.all(promises).then(function(results){
  console.log('results:', results);
});

An example of the results is as follows:The wait time is set randomly, so the result changes every time you run it.

%node-v
v5.5.0
% time node index.js
processing: 1099[msec] {a:'efg', b:456, c:true}
processing:1975[msec] {a:'abc', b:123, c:true}
processing:2648 [msec] {a:'hij', b:789, c:false}
results: [{a:'abc', b:123, c:true},
  { a: 'efg', b:456, c:true},
  { a: 'hij', b:789, c:false}]
node index.js 0.10s user 0.02s system 4% cpu 2.735 total

time is a command that prints the execution time as shown in the last line, and you can see that node index.js has the same total execution time as the most time-consuming process.

When you use Promise.race, subsequent actions (then or lower) are performed when you first complete as follows:

%node-v
v5.5.0
% time node index.js
processing:663 [msec] {a:'hij', b:789, c:false}
results: {a:'hij', b:789, c:false}
processing:727[msec] {a:'abc', b:123, c:true}
processing:2264 [msec] {a:'efg', b:456, c:true}
node index.js 0.08s user 0.02s system 4% cpu 2.333 total

In this case index.js, the last part is as follows:

Promise.race(promises).then(function(results){
  console.log('results:', results);
});

Note: Promise-JavaScript|MDN


2022-09-30 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.