2

Following scenario: A function gets 3 arrays of a certain length, each one of those needs to be iterated over to find a matching object. When the object is found, the for-loop breaks and the next one isn't called. The arrays can't be merged in this case. Basically like this:

for (let i = 0; i < array1.length; i++) {
  if (array1[i].id == matchingID) {
    returnValue = array1[i];
    break;
  }
}
if (!returnValue) {
  for (let i = 0; i < array2.length; i++) {
    if (array2[i].id == matchingID) {
      returnValue = array2[i];
      break;
    }
  }
}
if (!returnValue) {
  for (let i = 0; i < array3.length; i++) {
    if (array3[i].id == matchingID) {
      returnValue = array3[i];
      break;
    }
  }
}
return returnValue;

Would using promises be more efficient in this case, since that way all for-loops can be worked at at the same time? Like this, with each function-call doing one of the for-loops (and resolving the found value) from the example above:

return new Promise((resolve) => {
  this.findMatchingIDInArray1(array1)
    .then(() => resolve(returnValue));
  this.findMatchingIDInArray2(array2)
    .then(() => resolve(returnValue));
  this.findMatchingIDInArray3(array3)
    .then(() => resolve(returnValue));
}) 

Which way is more perfomant? Is there better way to do this? Thanks for your help!

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
TobiDevloft
  • 301
  • 3
  • 12
  • 5
    There's no reason to use promises because nothing in your function is asynchronous. – Pointy Sep 21 '18 at 13:32
  • 2
    Promises are doing nothing here as you have no async code. – Liam Sep 21 '18 at 13:32
  • 1
    You meant to use `array…[i].id`, right? – Bergi Sep 21 '18 at 13:38
  • 3
    You are performing linear search for O(n) in each `for` loop. If you're not changing *your algorithm*, you're not going to change that performance. If the arrays are sorted, then you can execute a binary search which *will* be faster. Then again, if the arrays don't have a lot of items, I don't think it would really matter that much. If you do have enormous amounts of data in each array, then a sort followed by a search optimised for sorted data could still give you a net performance boost. However, all that requires on *data* - don't do premature optimisation without testing first. – VLAZ Sep 21 '18 at 13:44
  • @Bergi yes, this is just an example that I wrote down really quick. Fixed! – TobiDevloft Sep 21 '18 at 13:48
  • @vlaz It doesn't go over huge amounts of data, it was just a question I asked myself after writing the code. Thanks for your input! – TobiDevloft Sep 21 '18 at 13:50
  • FYI, your example code is still broken. `array1[i].id = matchingID` is an assignment, not a comparison. In the future, please be sure your example code accurately reflects your question. – loganfsmyth Sep 22 '18 at 18:12

3 Answers3

7

Would using promises be more efficient in this case, since that way all for-loops can be worked at at the same time?

No, you misunderstood what promises do. They're a tool to make dealing with asynchronous code easier. There is no asynchronous code in your use case, so you cannot make use of promises here. Promises do not "make" anything asynchronous, or even enable multithreading-like parallelism.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
6

Would using promises be more efficient in this case, since that way all for-loops can be worked at at the same time?

No, because promises don't make anything asynchronous or parallel. They can only be used to observe things that are already asynchronous or parallel.

All that throwing promises at that code would do is change when the main thread blocks and add overhead.

If you need to offload or parallelize long-running computational tasks, look at web workers.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

The code would not be any more performant.

JavaScript is typically single-threaded (unless you use web workers), so your code wouldn't complete any more quickly using promises (because the loops wouldn't run in parallel)—in fact, if anything, it might be imperceptibly slower.

A promise is more of a way to handle the outcome of asynchronous code, and not a way to cause code to run asynchronously.

Alex Peters
  • 2,601
  • 1
  • 22
  • 29
  • Correct—I've already mentioned in my answer that “the code in question does not do this.” – Alex Peters Sep 21 '18 at 13:54
  • Fair point—it should say “if you made your looping code asynchronous...”. Updated. – Alex Peters Sep 21 '18 at 13:55
  • Yes, but if executed asynchronously, there's still a better chance (however small) that the execution won't take place at a critical time that the user would notice, no? – Alex Peters Sep 21 '18 at 13:57
  • 1
    Making it `async` alone still would block the UI. You would also need to inject some `await`s with millisecond timeouts that allow for something else to run. – Bergi Sep 21 '18 at 14:00
  • 1
    I've removed the third paragraph altogether. Appreciate the feedback; my understanding of `async` was a little flawed. – Alex Peters Sep 21 '18 at 14:06