Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of Javascript's reduce() function? (and map())

I'm trying to decide whether to use the reduce() method in Javascript for a function I need to write which is something like this

var x = [some array], y = {};
for (...) {
    someOperation(x[i]);
    y[x[i]] = "some other value";
}

Now this can obviously be written as a reduce() function in the following manner:

x.reduce(function(prev, current, index, arr) {
    someOperation(current);
    prev[current] = "some other value";
    return prev;
}, {})

Or something like that. Is there any performance or other difference between the two? Or some other reason (like browser support, for instance) due to which one should be favoured over the other in a web programming environment? Thanks.

like image 531
Jay Avatar asked Jun 16 '11 01:06

Jay


People also ask

What is the difference between map () filter () and reduce ()?

The map() function returns a new array through passing a function over each element in the input array. This is different to reduce() which takes an array and a function in the same way, but the function takes 2 inputs - an accumulator and a current value. So reduce() could be used like map() if you always .

What is the purpose of the reduce () function?

reduce() returns the value that is returned from the callback function on the final iteration of the array. reduce() is a central concept in functional programming, where it's not possible to mutate any value, so in order to accumulate all values in an array, one must return a new accumulator value on every iteration.

What is the difference between reduce and map in Python?

Python's reduce() function doesn't return a new sequence like map() and filter(). Instead, it returns a single value. The syntax is similar to the other two functions: reduce() applies the function to the elements of the sequence, from left to right, starting with the first two elements in the sequence.


2 Answers

Even though I prefer these operations (reduce, map, filter, etc.), it's still not feasible to use them because of certain browsers that do not support them in their implementations. Sure, you can "patch" it by extending the Array prototype, but that's opening a can of worms too.

I don't think there's anything inherently wrong with these functions, and I think they make for better code, but for now it's best not to use them. Once a higher percentage of the population uses a browser that supports these functions I think they'll be fair game.

As far as performance, these will probably be slower than hand written for loops because of the overhead from function calls.

like image 106
Cristian Sanchez Avatar answered Sep 21 '22 13:09

Cristian Sanchez


map and filter and reduce and forEach and ... (more info: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array#Iteration_methods ) are far better than normal loops because:

  1. They are more elegant
  2. They encourage functional programming (see benefits of functional programming)
  3. You will need to write functions anyway and pass into them, as parameters, iteration variables. This is because javascript has no block scope. Functions like map and reduce make your job so much easier because they automatically set up your iteration variable and pass it into your function for you.

IE9 claims to support these. They're in the official javascript/ecmascript spec. If you care about people who are using IE8, that is your prerogative. If you really care, you can hack it by overriding Array.prototype for ONLY IE8 and older, to "fix" IE8 and older.

like image 39
ninjagecko Avatar answered Sep 21 '22 13:09

ninjagecko