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.

3
  • 2
    a quick search returned this: stackoverflow.com/questions/3762589/… Commented Sep 29, 2015 at 21:27
  • 8
    4*3*2*1 is 24, not 10. Commented Sep 29, 2015 at 21:27
  • 1
    I've let the stack down. Commented Sep 29, 2015 at 21:28

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to do that, since Javascript does an implicit conversion.
@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.
@Buzinas oh wait that's stupid :) The same thing (NaN) will happen with the implicit conversion!
1

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

Comments

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.

1 Comment

Probably true! Though function calls are heavily optimized, a simple loop is almost certainly faster.
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);

2 Comments

Downvoted because the answer isn't right. The question was about speed and efficiency, using a for loop is substantially quicker.
@photoionized I see, that's true, thanks for informing :)
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("+"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.