map vs for-loop performance

JavaScript performance comparison

Revision 12 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var genericText = "Generic prepended text, calculated value is: ";
    var arr = new Array();
    for (var i = 0; i < 10000; i++) {
      arr[i] = i;
    }
    var $arr = $(arr);
    var newArr = [];
   
    function multReturn(x) {
      return x * x;
    }
   
    function map(items, func) {
      var results = [],
        i;
      for (i = 0; i < items.length; i++) {
        results.push(func(items[i]));
      }
      return results;
    }
};

Benchmark.prototype.teardown = function() {
    newArr = [];
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
map
newArr = arr.map(multReturn);
pending…
for
for (var i = 0; i < arr.length; i++) {
  newArr[i] = multReturn(arr[i]);
}
pending…
$.each
$.each(arr, function(i, e) {
  newArr[i] = multReturn(e);
});
pending…
$arr.each
$arr.each(function(i, e) {
  newArr[i] = multReturn(e);
});
pending…
for decrementing
for (var i = arr.length - 1; i >= 0; i--) {
  newArr[i] = multReturn(arr[i]);
}
pending…
myMap
map(arr, multReturn);
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