0

I'm perplexed why a previous engineer wrote some code that looks like double work to me. Could you not just do either the accounts.map() or the for (const acct of accounts)? Why both?

const accounts = await mongohelper.find(conn, 'accounts', query, 2000);
if(!accounts.length){return;}
const arrayofwork = accounts.map(act => salesmanwork(act, conn))


for (const act of accounts) {
    arrayofwork.push(salesmanwork(act, conn));
}

await Promise.allSettled(arrayofwork)
return await recursivecall(workitemin, count);
Alphonso77
  • 67
  • 9
  • 4
    It does look like a duplication of effort – evolutionxbox Dec 17 '21 at 15:48
  • 1
    "*I'm perplexed why a previous engineer wrote some code that looks like double work to me.*" could be a bug. Could also be a merge issue - e.g., the code was converted from a loop to `.map()` and a merge re-introduced the deleted code. – VLAZ Dec 17 '21 at 15:51
  • maybe that is the reason why he doesn't work there anymore :) – Flash Thunder Dec 17 '21 at 17:02

2 Answers2

0

It's making a duplication of the processed values from the accounts array by salesmanwork function. Maybe that's what's intended or might be a bug, some code that they didn't remove.

AntennaeVY
  • 26
  • 3
0

Output is basically the same, the only differences are speed, readability and functionality, which barely differ. What I mean by this is that when using .map() it returns an array, in where you can use it in one-liners and promises, while in for loops, you need to initialize, then make the loop, and return the same value.

In this case, it is probably some code someone forgot to clean up; either works, the only differences are readability. If you are using a bigger array, use the for loop, otherwise it's up to you.

insyri
  • 308
  • 2
  • 8
  • Thanks, insyri, this is a function that gets called several hundred times in a matter of seconds. The promises are database calls to mongo. I'm sure mongo is probably caching things, but I'm hoping there will be a performance gain by cutting the db calls by 1/2. – Alphonso77 Dec 17 '21 at 19:25
  • No problem, also for some speed optimization check out this and see if this could fit in: https://stackoverflow.com/questions/21034662/why-push-method-is-significantly-slower-than-putting-values-via-array-indices-in – insyri Dec 18 '21 at 01:09