1

I'd like to know the fastest and most efficient way to multiply array elements together using javascript.

var array = ["1", "2", "3", "4"];

So the outcome would total 10.

Buzinas
  • 11,597
  • 2
  • 36
  • 58
DIM3NSION
  • 1,681
  • 5
  • 20
  • 38

5 Answers5

5

First, those values are strings, not numbers, so the first thing to do is make them numbers:

 array.map(Number)

Then multiply with .reduce

 array.map(Number).reduce(function(product, value) { return product * value; });

edit — a comment wisely notes that the * operator will try to convert its operands to numbers anyway. With the explicit conversion to numbers, you could add a filter to see if there were any NaN values produced, but NaN spreads like cancer so it really wouldn't matter. Consider the numeric conversion optional.

Now, in general, if I were getting an array of numbers-as-strings from an API, I'd be inclined to do an explicit conversion, because in a sense that means that there's really something wrong with the design of the API. I'd prefer to isolate any "interesting" code from weird stuff like that.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • You don't need to do that, since Javascript does an implicit conversion. – Buzinas Sep 29 '15 at 21:35
  • @Buzinas hey you're right! Still I'd be inclined to do it if I knew I had an array of strings that were *supposed* to be numbers, but might not be. – Pointy Sep 29 '15 at 21:36
  • @Buzinas oh wait that's stupid :) The same thing (`NaN`) will happen with the implicit conversion! – Pointy Sep 29 '15 at 21:37
1

The most efficient/performant way will be always to use regular loop:

var array = ['1', '2', '3', '4'], i = array.length, result = 1;
while (i > 0)
  result *= array[--i];

But since that kind of performance will never matter in the real world, I suggest you to use reduce:

var array = ['1', '2', '3', '4'];

var result = array.reduce(function(a, b) {
  return a * b;
});

console.log(result);
Buzinas
  • 11,597
  • 2
  • 36
  • 58
1

The fastest form of loop is standard for, See map vs for-loop performance test.

NOTE : The slower is map() function like you can see in test.

var Array = ["1", "2", "3", "4"];
var total=1;

for (var i = 0; i < Array.length; ++i) {
    total *= Array[i];
}

alert(total);

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • 1
    Probably true! Though function calls are heavily optimized, a simple loop is almost certainly faster. – Pointy Sep 29 '15 at 22:06
1

Fast and efficient, go with a good old for loop, see http://jsperf.com/reduce-v-for-javascript

photoionized
  • 5,092
  • 20
  • 23
0

You can use reduce

var sum = array.reduce(function(pv, cv) { return pv + cv; }, 0);

with arrow functions in ES6, it is even simpler.

sum = array.reduce((pv, cv) => pv+cv, 0);

or with for loop

var total = 0;
for (var i = 0, n = array.length; i < n; ++i){
 total += array[i];
}

and with crazy way

var a = [1,2,3,4,5];
sum = eval(a.join("+"));
Haider Ali
  • 227
  • 3
  • 17