What's the correct way to iterate over an array of objects in ES6 parallelly?


Write your response…

This answer has received 1 appreciation.

Something like this?

myArray.map(async entry => {
  // do something async

This is much as parallel as you can get now.

You can collect the results with Promise.all().

Write a reply...

This answer has received 2 appreciations.

First, any implementation using forEach,map etc will at best be 'wrappers' as they are themselves not async & parallel..

I can foresee a path where you might achieve parallelism by sub dividing your iterable (array or whatever) into sub-parts and then 'sequentially' iterate on those sub-parts and then combine them within a promise or another object.

That being said I liked the question so I did a strange experiment. This is sequential, but unique in the sense that it uses both microtask queues:

Array.prototype.asyncIterate = async function(f,i,l){
    i = i || 0;
    var copy = i ? this : this.slice(),
        d = copy[i];
    l = l || copy.length;
    await (async function(copy,d,i){window.requestAnimationFrame(function(){
    if(i === l-1) {
        return true;
    return copy.asyncIterate(f,++i,l);

now you can:

var x = [1,2,3,4];

The above will not mutate the array it is called on, and will return a resolved promise to true in the end. Did not do a performance test :) Eventually if you make another function and slice the main array into subparts and call asyncIterate you might achieve the effect you want.

Also don't change native prototypes, I just did it to explain this case only.

Write a reply...

In parallel? Currently, native JS does not have SIMD instructions or similar to iterate over data in parallel. What you can do, however:

const arr = [
  { foo: 1 },
  { foo: 2 },
  { foo: 3 },

// blocking (sync)
  obj => console.log(obj.foo)

// non-blocking (async)
  obj => setTimeout(() => console.log(obj.foo), 0)

Well, if you want to iterate over an array in parallel (and possibly do an operation on each item), and want to use WebDev and not limit yourself to JS only, you can do so by using WASM, which allows you to either use native SIMD (C, C++) or multithreading (C, C++, Rust).

Write a reply...

The concept of a single "correct way" is a flawed one -- in JS there are always multiple ways of doing things and each is best suited to a specific type of data and a specific type of output.

That said, I'm wondering how you expect anything done in parallel in an inherently single threaded language. I mean you can fake the behavior, and there's the specification train wreck of promises, but if this is data you already have there's no point in trying to parallelize anything.

So I'd be throwing a question back at you -- what's the data and what are you doing to it? THAT determines the proper answer.

Siddarthan Sarumathi Pandian To me that is still too 'vague' a description of what is being done. I would still be asking WHY not sequential? ...and even if so how and what that data is and WHY it is being processed out of order / parallel would be the determining factor of how I'd go about it.

What is being put in and what's being done with it is how you make your choices. Simply saying "API call" and "objects" is uselessly and pointlessly vague. WHAT objects? WHAT API? What mechanism of passing? What processing?

Write a reply...

Join a friendly and inclusive Q&A network for coders

  • 🖥Pick the technologies you like & read great content through your feed.
  • 💬Ask a question when you want to learn more about anything.
  • 🚀Share what you know & build your portfolio.
Sign up nowLearn more

loading ...