map vs for-loop performance

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code

<script src="https://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 genericText + Math.sqrt(x * x);
  }

};

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 CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
map
newArr = arr.map(multReturn);
pending…
for
for (var i = 0; i < arr.length; i++) {
  var x = arr[i];
  newArr[i] = genericText + Math.sqrt(x * x);
}
pending…
$.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…

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.

0 Comments