map vs native for loop

JavaScript performance comparison

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


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

Preparation code

<script src=""></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) {
    mapJQuery = function(array, mapFunction) {
      return $.map(array, mapFunction);

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);
number for loop map
var result = mapForLoop(testArray, numberOp);
number built in js map
var result = mapBuiltIn(testArray, numberOp);
string jquery map
var result = mapJQuery(testArray, stringOp);
string for loop map
var result = mapForLoop(testArray, stringOp);
string built-in map
var result = mapBuiltIn(testArray, stringOp);

Compare results of other browsers


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:


Liam Newman commented :

I've split out the string map into a separate test for easier comparison. see

Benjamin commented :

I'm not surprised about, but Javascript's native map function should be much faster I would have thought. Goes to show you can't always go by gut feelings - yay jsperf!

Comment form temporarily disabled.

Add a comment