Use promise all() for expensive computations in javascript?

Viewed 181

Is there any point to use promise all() to speed up an expensive computation ? For example:

// Expensive computation

const data = [1, 2, 3];

const f = (x) => {
  return x*x; // or a more complex formula
}

// No promises

const f_data = data.map(f);

// With promises

const f_data = Promise.all(data.map((e) => Promise((e) => f(e))).then(res => res).catch(...);

Will there be any actual difference in execution speed ?

1 Answers

No, if you want to speed up computations you have to use workers in order to leverage multithreading.

If you use Promise.all, all of the callbacks will be queued and will run in a single thread, one after the other.

Related