map vs native for loop (v44)

Revision 44 of this benchmark created by Omar Saqr on


Description

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

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

testArray = new Array(100);
    for (var i = 0; i < 100; i++) {
      testArray[i] = i;
    }
    
    forMap = function(array, mapFunction) {
      var newArray = new Array(array.length);
      for (var i = 0,l=array.length; i < l; i++) {
        newArray[i] = mapFunction(array[i]);
      }
    
      return newArray;
    }
    
    forMap2 = function(array, mapFunction) {
      var newArray = [];
      for (var i = 0,l=array.length; i < l; i++) {
        newArray.push(mapFunction(array[i]));
      }
    
      return newArray;
    }
    
    whileMap = function(array, mapFunction) {
      var i = array.length;
      var newArray = new Array(i);
    
      while (i--) {
        newArray[i] = mapFunction(array[i]);
      }
    
      return newArray;
    }
    whileMap2 = function(array, mapFunction) {
      var i = array.length;
      var newArray = [];
    
      while (i--) {
        newArray.push(mapFunction(array[i]));
      }
    
      return newArray;
    }
    
    
    double = function(x) {
      return x * 2;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jquery map
var result = $.map(testArray, double);
ready
for loop map
var result = forMap(testArray, double);
ready
for loop
var result = Array(testArray.length);

for (var i = 0; i < testArray.length; i++) {
  result[i] = double(testArray[i]);
}
ready
array.map
var result = testArray.map(double);
ready
while loop
var result = Array(testArray.length);
var i = testArray.length;

while(i--) {
  result[i] = double(testArray[i]);
}
ready
while loop map
var result = whileMap(testArray, double);
ready
while loop map with array push
var result = whileMap2(testArray, double);
ready
for loop map with array push
var result = forMap2(testArray, double);
ready

Revisions

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