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

JavaScript performance comparison

Test case created by Jordan and last updated

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="https://code.jquery.com/jquery-1.5.min.js"></script>
<script>
var nativeMap = Array.prototype.map;
</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 = 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);
  }
</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
var ks = $.map([1, 2, 4, 6], function(v, k) {
 return k;
});
pending…
New $.map (object support and forces everything to run through $.each)
var ks = $.new_map([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