Old $.map vs new $.map with obj support

JavaScript performance comparison

Revision 3 of this test case created by Jordan

Preparation code

<script src="https://code.jquery.com/jquery-1.5.min.js"></script>
<div>something</div>
<div>another</div>
<div>something else</div>
<div>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the lea</div>
<script>
  /*
  Original $.map function in the jQuery core
  // arg is for internal usage only
  	map: function( elems, callback, arg ) {
  		var ret = [], value;
  
  		// Go through the array, translating each of the items to their
  		// new value (or values).
  		for ( var i = 0, length = elems.length; i < length; i++ ) {
  			value = callback( elems[ i ], i, arg );
  
  			if ( value != null ) {
  				ret[ ret.length ] = value;
  			}
  		}
  
  		return ret.concat.apply( [], ret );
  	}
  
  */
  $.new_map_each = function(elems, callback, arg) {
  
   var ret = [],
       value;
  
   $.each(elems, function(key, value) {
    value = callback(value, key, arg);
    if (value != null) {
     ret[ret.length] = value;
    }
   });
  
   return ret.concat.apply([], ret);
  }
  
  $.new_map_forin = function(elems, callback, arg) {
   var ret = [],
       value;
  
   // Go through the array, translating each of the items to their
   // new value (or values).
   if (jQuery.isPlainObject(elems)) {
    for (name in elems) {
     value = callback(elems[name], name, arg);
     if (value != null) {
      ret[ret.length] = value;
     }
    }
   }
   else {
    for (var i = 0, length = elems.length; i < length; i++) {
     value = callback(elems[i], i, arg);
  
     if (value != null) {
      ret[ret.length] = value;
     }
    }
   }
  
   return ret.concat.apply([], ret);
  }
  
  $.new_map_forin_2 = function(elems, callback, arg) {
   var ret = [],
       value, length = elems.length;
   isObj = length === undefined;
  
   // Go through the array, translating each of the items to their
   // new value (or values).
   if (isObj) {
    for (name in elems) {
     value = callback(elems[name], name, arg);
     if (value != null) {
      ret[ret.length] = value;
     }
    }
   }
   else {
    for (var i = 0; i < length; i++) {
     value = callback(elems[i], i, arg);
  
     if (value != null) {
      ret[ret.length] = value;
     }
    }
   }
  
   return ret.concat.apply([], ret);
  }
  
  $.new_map_forin_3 = function(elems, callback, arg) {
   var ret = [],
       value;
  
   // Go through the array, translating each of the items to their
   // new value (or values).
   if (!$.isArray(elems)) {
    for (name in elems) {
     value = callback(elems[name], name, arg);
     if (value != null) {
      ret[ret.length] = value;
     }
    }
   }
   else {
    for (var i = 0, length = elems.length; i < length; i++) {
     value = callback(elems[i], i, arg);
  
     if (value != null) {
      ret[ret.length] = value;
     }
    }
   }
  
   return ret.concat.apply([], ret);
  }
</script>
    

Preparation code output

<div>something</div> <div>another</div> <div>something else</div> <div>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the lea</div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Old $.map (no obj support)
var ks = $.map([1, 2, 4, 6], function(v, k) {
 return k;
});
pending…
$.new_map_each (obj support with $.each under the hood)
var ks = $.new_map_each([1, 2, 4, 6], function(v, k) {
 return k;
});
pending…
$.new_map_forin (obj support with for-in instead of each - detects with isPlainObject func)
var ks = $.new_map_forin([1, 2, 4, 6], function(v, k) {
 return k;
});
pending…
$.new_map_forin_2 (obj support with for-in instead of each.. detects obj without isPlainObject func)
var ks = $.new_map_forin_2([1, 2, 4, 6], function(v, k) {
 return k;
});
pending…
$.new_map_forin_3 (obj detection using isArray)
var ks = $.new_map_forin_3([1, 2, 4, 6], function(v, k) {
 return k;
});
pending…

Compare results of other browsers

Revisions

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

0 Comments

something
another
something else
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the lea