looping

JavaScript performance comparison

Revision 4 of this test case created

Info

I have an array, I want a new array where I do something to it, how do I do it fastest.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var _len = 10000;
    Array.prototype.cMap=function(func,context){
        var i = 0;
        var len = this.length;
        var out = new Array(len);
        while (i < len) {
          out[i] = func.call(context,this[i],i,this);
          i++
        }
        return out;
    }
    var a = new Array(_len);
   
    for (var i = 0; i < _len; i++) {
      a[i] = i;
    }
   
   
    var f = function(a) {
        return a * a;
        }
};
</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
reg
var i = 0;
var len = a.length;
var out = new Array(len);
while (i < len) {
  out[i] = f(a[i]);
  i++
}
pending…
map
var out = a.map(f);
pending…
custom map
var out = a.cMap(f);
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