map named functions (v14)

Revision 14 of this benchmark created on


Description

I wanted to check if named functions are better than anonymous functions. Plus other comparisons.

Setup

var data = Array.apply(null, Array(10)).map(function(_, i) {
      return i;
    });
    
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Polyfill
    Array.prototype.map2 = function(fun /*, thisArg */ ) {
      "use strict";
    
      if (this === void 0 || this === null)
        throw new TypeError();
    
      var t = Object(this);
      var len = t.length >>> 0;
      if (typeof fun !== "function")
        throw new TypeError();
    
      var res = new Array(len);
      var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
      for (var i = 0; i < len; i++) {
        // NOTE: Absolute correctness would demand Object.defineProperty
        //       be used.  But this method is fairly new, and failure is
        //       possible only if Object.prototype or Array.prototype
        //       has a property |i| (very unlikely), so use a less-correct
        //       but more portable alternative.
        if (i in t)
          res[i] = fun.call(thisArg, t[i], i, t);
      }
    
      return res;
    };
    
    // My own simple implementation
    Array.prototype.map3 = function(callback /*, thisArg */ ) {
      'use strict';
      if (typeof callback !== 'function') {
        throw new TypeError();
      }
    
      var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    
      for (var i = 0, len = this.length; i < len; i++) {
        this[i] = callback.call(thisArg, this[i], i, this);
      };
    };
    
    // DANNO
    Array.prototype.map4 = function(callback /*, thisArg */ ) {
      'use strict';
      if (typeof callback !== 'function') {
        throw new TypeError();
      }
    
      var curr;
      for (var i = 0, len = this.length; i < len; i++) {
        curr = this[i];
        this[i] = callback(curr);
      };
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Map anonymous
data.map(function(val) {
  return val + 1;
});
ready
Map named
var f = function(val) {
  return val + 1;
};
data.map(f);
ready
for simple
for (var i = 0, len = data.length; i < len; i++) {
  data[i] = data[i] + 1;
}
ready
for in
for (var i in data) {
  data[i] = data[i] + 1;
}
ready
while
var count = data.length;
while (count > 0) {
  data[count - 1] = data[count - 1] + 1;
  count--;
}
ready
Map2 anonymous
data.map2(function(val) {
  return val + 1;
});
ready
Map2 named
var f = function(val) {
  return val + 1;
};
data.map2(f);
ready
Map3 anonymous
data.map3(function(val) {
  return val + 1;
});
ready
Map3 named
var f = function(val) {
  return val + 1;
};
data.map3(f);
ready
Map4 anonymous
data.map4(function(val) {
  return val + 1;
});
ready
Map4 named
var f = function(val) {
  return val + 1;
};
data.map4(f);
ready

Revisions

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