3
\$\begingroup\$

I am working on rewriting the underlying code for some Underscore.js functions. Re-writing the reduce function has been causing a lot of headaches. I have been able to re-write it two different ways, both work, but I was wondering which version is more efficient and if I could improve either one?

Reduce function one:

function reduce(collection, func, accumulator) {
            var i = 0;
            var isCollection = Array.isArray(collection);
            if (arguments.length < 3) {
                isCollection ? accumulator = collection[0] : Object.keys(collection)[0];
                i++;
            }

            if (isCollection) {
                for (; i < collection.length; i++) {
                    accumulator = func(accumulator, collection[i]);
                }
            } else {
                for (var key in collection) {
                    accumulator = func(accumulator, collection[key]);
                }
            }

            return accumulator;
        };

var numbers = [1, 2, 3];


var sum = reduce(numbers, function(total, number){
         return total + number;
       }, 0); // should be 6
       
console.log(sum); 

Reduce function two:

      function reduceTwo(collection, func, accumulator) {
          var args = arguments.length < 3;

          each(collection, (item) => {
              if (args) {
                  accumulator = item;
                  args = false;
              } else {
                  accumulator = func(accumulator, item);
              }
          })
          return accumulator;

      };

      function each(collection, iterator) {
          if (Array.isArray(collection)) {
              for (var i = 0; i < collection.length; i++) {
                  iterator(collection[i], i, collection);
              }
          } else {
              for (var key in collection) {
                  iterator(collection[key], key, collection);
              }
          }

      };
      var numbers = [1, 2, 3]
      var sum = reduceTwo(numbers, function(total, number) {
          return total + number;
      }, 0); // should be 6

      console.log(sum)

EDIT: 1st function does not work on objects so rewrote it and now it works with objects.

var numbers = {a: 1, b: 2, c:3};
       var sum = reduce(numbers, function(total, number){
         return total + number;
       });

function reduce (collection, func, accumulator) {
        var i = 0;
        var isCollection = Array.isArray(collection);
        var keys = Object.keys(collection);
        var length = isCollection ? collection.length : keys.length;

        if (arguments.length < 3) {
            isCollection ? accumulator = collection[0] : accumulator = collection[keys[0]];
            i++;
        }

          for (; i < length; i++) {
                isCollection ? accumulator = func(accumulator, collection[i]) : accumulator = func(accumulator, collection[keys[i]]);
            }


        return accumulator;
    };

console.log(sum);

\$\endgroup\$
  • \$\begingroup\$ So I am working on re-writing the underlying code for some Underscore.js functions. Ok, but why? Just curious \$\endgroup\$ – Flambino Mar 8 '17 at 21:15
  • 2
    \$\begingroup\$ @Flambino practice makes perfect. I'm learning JS so I thought it would be a good way to practice coding. \$\endgroup\$ – A_A Mar 8 '17 at 21:37
  • \$\begingroup\$ Cool - and yes, fine way to practice! \$\endgroup\$ – Flambino Mar 8 '17 at 21:38
  • \$\begingroup\$ Welcome to Code Review! I've added the comparative-review tag since you want to get more than one approach reviewed. Hope you get great answers! \$\endgroup\$ – Phrancis Mar 8 '17 at 22:04
  • 1
    \$\begingroup\$ Why are you asking us which is more efficient? That is not the domain of a code review to determine, that is the domain of actual performance testing. Test your implementations against various test data structures and find out which is more efficient (or which is more efficient for different use cases even). \$\endgroup\$ – Mike Brant Mar 9 '17 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.