javascript - Is it more performant to use several for-loops by themself or to delegated the logic to promises? - Stack Overflow

Following scenario: A function gets 3 arrays of a certain length, each one of those needs to be iterate

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!

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!

Share Improve this question edited Sep 22, 2018 at 18:12 loganfsmyth 162k31 gold badges346 silver badges258 bronze badges asked Sep 21, 2018 at 13:31 TobiDevloftTobiDevloft 3013 silver badges12 bronze badges 7
  • 5 There's no reason to use promises because nothing in your function is asynchronous. – Pointy Commented Sep 21, 2018 at 13:32
  • 2 Promises are doing nothing here as you have no async code. – Liam Commented Sep 21, 2018 at 13:32
  • 1 You meant to use array…[i].id, right? – Bergi Commented Sep 21, 2018 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 Commented Sep 21, 2018 at 13:44
  • @Bergi yes, this is just an example that I wrote down really quick. Fixed! – TobiDevloft Commented Sep 21, 2018 at 13:48
 |  Show 2 more ments

3 Answers 3

Reset to default 8

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.

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 putational tasks, look at web workers.

The code would not be any more performant.

JavaScript is typically single-threaded (unless you use web workers), so your code wouldn't plete 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 oute of asynchronous code, and not a way to cause code to run asynchronously.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745395087a4625841.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信