for vs map

JavaScript performance comparison

Test case created by JacksonTian

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var list = [];
    for (var i = 0; i < 10000; i++) {
      list[i] = i;
    }
    var mymap = function (arr, callback) {
      var ret = [];
      for (var i = 0, l = arr.length; i < l; i++) {
        ret.push(callback(arr[i]));
      }
      return ret;
    };
};
</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
list.map(function (item) {
  return item * 2;
});
 
pending…
for push
var ret = [];
for (var i = 0, l = list.length; i < l; i++) {
  ret.push(list[i] * 2);
}
 
pending…
for index
var ret = [];
for (var i = 0, l = list.length; i < l; i++) {
  ret[i] = list[i] * 2;
}
pending…
mymap
  mymap(list, function (item) {
    return item * 2;
  });
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