Native Array map vs. emulated (v2)

Revision 2 of this benchmark created by Ryan Grove on


Preparation HTML

<script src="http://fuji.jetpants.com/yui/devcombo/yui3-dev?build/yui/yui-min.js&build/loader/loader-min.js"></script>
<script>
  var Y = YUI({
   comboBase: 'http://fuji.jetpants.com/yui/combo/yui3-dev?',
   combine: true,
   root: 'build/'
  }).use('array-extras'),
      states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"],
      results;
  
  // YUI3-style emulated each().
  
  function each(a, f, o) {
   var l = (a && a.length) || 0,
       i;
   for (i = 0; i < l; i = i + 1) {
    f.call(o || this, a[i], i, a);
   }
  }
  
  function map(array, callback, thisObject) {
   var i = 0,
       len = array.length,
       results = [];
  
   for (; i < len; ++i) {
    results.push(callback.call(thisObject, array[i], i, array));
   }
  
   return results;
  }
  
  function map_dupe(array, callback, thisObject) {
   var i = 0,
       len = array.length,
       results = array.concat();
  
   for (; i < len; ++i) {
    results[i] = callback.call(thisObject, array[i], i, array);
   }
  
   return results;
  }
  
  // YUI3-style emulated map().
  
  function map_each(array, callback, thisObject) {
   var results = [];
   each(array, function(item, i, a) {
    results.push(callback.call(thisObject, item, i, a));
   });
   return results;
  }
  
  function callback(item) {
   return item + 'pants';
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Native map
results = states.map(callback);
ready
Emulated map #1 (new array + push)
results = map(states, callback);
ready
Emulated map #2 (dupe array + replace)
results = map_dupe(states, callback);
ready
Emulated map #3 (YUI 3.2.0-style)
results = map_each(states, callback);
ready
YUI3 dev build (native/emulated based on feature test)
results = Y.Array.map(states, callback);
ready

Revisions

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

  • Revision 1: published by Ryan Grove on
  • Revision 2: published by Ryan Grove on