map vs native for loop

JavaScript performance comparison

Revision 7 of this test case created by Liam Newman and last updated

Info

test the performance of the jquery map, built-in Array.map, and native for loop map

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    testArray = new Array(500);
    for(var i = 0; i < 500; i++) {
      testArray[i] = i;
    }
   
    numberOp = function(x, y, z) {
      return x*y;
    }
   
    stringOp = function(x, y, z) {
      return (x.toString() + Math.random().toString());
    }
   
   
    mapForLoop = function(array, mapFunction) {
      var arrayLen = array.length;
      var newArray = new Array(arrayLen);
      for(var i = 0; i < arrayLen; i++) {
        newArray[i] = mapFunction(array[i], i, array);
      }
   
      return newArray;
    }
   
    mapBuiltIn = function(array, mapFunction) {
      return array.map(mapFunction);
    }
   
    mapJQuery = function(array, mapFunction) {
      return $.map(array, mapFunction);
    }
   
};
</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
number jquery map
var result = mapJQuery(testArray, numberOp);
pending…
number for loop map
var result = mapForLoop(testArray, numberOp);
pending…
number built in js map
var result = mapBuiltIn(testArray, numberOp);
pending…
string jquery map
var result = mapJQuery(testArray, stringOp);
pending…
string for loop map
var result = mapForLoop(testArray, stringOp);
pending…
string built-in map
var result = mapBuiltIn(testArray, stringOp);
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:

1 comment

Liam Newman commented :

I've split out the string map into a separate test for easier comparison. see http://jsperf.com/map-vs-for-loop-string

Add a comment