ArrayPerfTest

JavaScript performance comparison

Revision 5 of this test case created

Info

Litixsoft

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var data = [];
    var result = null;
   
    for (var i = 0; i < 10000; i++) {
      data[i] = i;
    }
   
    var myForEach = function(obj, action, context) {
        for (i = 0, length = obj.length; i < length; i++) {
          action.call(context, obj[i]);
        }
    }
};
</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
For Bad
for (var y = 0; y < data.length; y++) {
  result += y;
}

result = 0;
pending…
For Each
data.forEach(function(item) {
  result += item;
});

result = 0;
pending…
For Good
var x = 0;
var length = data.length;

for (x = 0; x < length; x++) {
  result += x;
}

result = 0;
pending…
For Opt
var z = 0;
var length1 = data.length;

for (z = 0; z < length1; z += 1) {
  result += z;
}

result = 0;
pending…
reduce
result = data.reduce(function(res, item) {
  return res + item;
}, 0);

result = 0;
pending…
forEach self made
result = myForEach(data, function(item) {
  result += item;
});

result = 0;
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