Lo-Dash Deep Map 2 Level vs. Recursive n Level

Benchmark created by pwhty on


Description

Check how a (IMO confusing) two level nested _.map (or any fixed level for that matter - it just makes it even more confusing) performs compared to an arbitrarily level _.map that uses recursion.

Preparation HTML

<script src="https://cdn.jsdelivr.net/lodash/2.4.1/lodash.min.js"></script>

Setup

// Recursive _.map implementation for deep mapping
    _.mixin({
      'deepMap': function(collection, callback, thisArg) {
        callback = _.createCallback(callback, thisArg, 3);
        return _.map(collection, function(value, index) {
          // if it's a number, map it to the requested object {x: index, y: value}
          if (_.isNumber(value)) {
            return callback(value, index);
          }
          // if it's an array, map recursively inside it
          if (_.isArray(value))
            return _.deepMap(value, callback);
          return value;
        });
      }
    });
    
    // Fixed implementation for two dimensional mapping
    _.mixin({
      'twoLvlMap': function(collection, callback, thisArg) {
        callback = _.createCallback(callback, thisArg, 3);
        // loop through all elements of collection and map them to...
        return _.map(collection, function(item) {
          // ...the result from running the mapping function on all elements at second level
          return _.map(item, callback);
        });
      }
    });
    
    // Fixed implementation for three dimensional mapping
    _.mixin({
      'threeLvlMap': function(collection, callback, thisArg) {
        callback = _.createCallback(callback, thisArg, 3);
        // loop through all elements of collection and map them to...
        return _.map(collection, function(item) {
          // the result of looping through all elements of collection and mapping them to...
          return _.map(item, function(item) {
            // ...the result from running the mapping function on all elements on third level
            return _.map(item, callback);
          });
        });
      }
    });
    
    var testArray2Lvl = [
      [1, 2, 3],
      [11, 12, 13],
      [21, 22, 23]
    ];
    var testArray3Lvl = [
      [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
      ],
      [
        [11, 12, 13],
        [14, 15, 16],
        [17, 18, 19]
      ],
      [
        [21, 22, 23],
        [24, 25, 26],
        [27, 28, 29]
      ]
    ];
    
    // Elements on the lowest (leaf) level should be mapped t an object
    var testCallback = function(value, index) {
      return {
        x: parseInt(index),
        y: value || 0
      };
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Two Level Nested
_.twoLvlMap(testArray2Lvl, testCallback);
ready
Two Level Recursive
_.deepMap(testArray2Lvl, testCallback);
ready
Three Level Nested
_.threeLvlMap(testArray3Lvl, testCallback);
ready
Three Level Recursive
_.deepMap(testArray3Lvl, testCallback);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by pwhty on