How to optimize the sum of elements in an array

Asked

Viewed 2,010 times

10

I need to get the sum of the items of a array javascript.

The quantity of these items can easily reach 2,000 items. The items are of the type int, no need to test.

ar = [1,3,5,...,2000];

I already have one array containing the elements, I do not need to take the value in inputs. I want to add the elements of this array, in a clear and objective way.

That way I’ll be doing it right? I’ll lose using lasso?

for (f=0;f<60;f++)
{
  valorSoma = ar[f]+valorSoma;
}

3 answers

11


If you want the most performative way:

var ar = [1, 3, 5, 8, 2000];
for (var i = 0, total = 0; i < ar.length; total += ar[i++]);
console.log(total);

I put in the Github for future reference.

My result comparing for, for with length external (contrary to popular belief is slower than leaving internal), reduce() and forEach():

benchmark

So your way is correct, even though you’re only taking 60 items and it’s not written in the best possible way. I made it as short as possible (since the comparison seems to be with using a ready function) without losing performance.

  • I can’t be sure which to use: for or reduce(). I don’t have all this theoretical baggage, but you really need to choose the best solution. I get to have 20 arrays with 2,000 items, and I need to add them.

  • If you have enough and need the performance for is more interesting.

  • 1

    Does Jsperf have this comparative?

  • @durtto tem: https://jsperf.com/foreach-vs-reduce-vs-for-loop. I don’t know if the same is true there, but in my browser, contrary to popular belief, leave the length in the loop is faster than extracting from it. Related: http://answall.com/q/152521/101

  • Yes, you’re right. reduce() is slower than filter().

  • http://jsbench.github.io/#a9a0d85633a1a589e7e2ae4e01dfe13e

  • @durtto this comparison uses the 3 worst ways to make :)

  • yes, but even so gets in the front. Interesting as not always native javascript functions are faster than manual implementations.

  • The last loop (in which the length property of the object is reserved in a variable) may be faster in older browsers, I believe that modern browsers currently reserve the size of an array in memory, and when they are modified they only need to update this size again

  • @Someone undoubtedly.

Show 5 more comments

9

From ES6, you can use Array.reduce so with arrow functions:

var numeros = [1, 3, 4, 2000];
var soma = numeros.reduce((a, b) => a + b);

console.log(soma);

Another alternative with while:

var numeros = [1, 3, 4, 2000];
var tamanho = numeros.length;
var total = 0;

while(tamanho--) {
  total += numeros[tamanho];
}

console.log(total);

6

Use reduce

var meuArray = [1, 2, 3, 200];

var countArray = meuArray.reduce(function(total, item) {
    return total+item;
}, 0);

alert(countArray);

See how reduce works on W3schools, and a great video to learn that too funfunction

  • I edited the question changing the direction, want to adjust your answer?

  • 1

    +1 by the funfunfunction link

  • No, it’s been well answered above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.