map vs native for loop

JavaScript performance comparison

Revision 17 of this test case created

Info

test the performance of the jquery map versus a native for loop map

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var v;
   
    testArray = new Array(100);
    for(var i = 0; i < 100; i++) {
      testArray[i] = i;
    }
   
    map = function(array) {
      v = 0;  
      for(var i = 0; i < array.length; i++) {
        v += array[i];
      }
    }
   
    map2 = function(array) {
      v = 0;
      var length = array.length;
      for(var i = 0; i < length ; i++) {
        v += array[i];
      }
    }
   
    map3 = function(array) {
      v = 0;
      for(var i in array) {
        v += array[i];
      }
    }
   
    map4 = function(array, fun)
    {
        var i = 0,
            len = array.length;
   
        while(i<len)
        {
            fun(i);
            i++;
        }
    }
   
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jquery map
v = 0;
var result = $.map(testArray, function(i){v += i;});
pending…
for loop
var result = map(testArray);
pending…
clever for loop
var result = map2(testArray);
pending…
js map
v = 0;
var result = testArray.map(function(i){v += i;});
pending…
iterate
var result = map3(testArray);
pending…
Map 4
v = 0;
var result = map4(testArray, function(i){v += i;});
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. Here’s a list of current revisions for this page:

0 comments

Add a comment