objdir

JavaScript performance comparison

Revision 7 of this test case created by Tim

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var range = function(min, max, diff) {
        "use strict";
        var returnList = [];
        i = min;
        if (!diff) {
          diff = 1;
        }
        for (; i < max; i = i + diff) {
          returnList.push(i);
        }
        return returnList;
        },
        obj = {},
        data = range(1, 10000),
        i = 0;
    for (; i < data.length; i += 1) {
      obj[i] = i + 1;
    }
};
</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 in
for (var a in obj) {
  var t = (a + "->" + obj[a]);
}
pending…
for in cached
var a;
for (a in obj) {
  var t = (a + "->" + obj[a]);
}
pending…
Object prototype
Object.keys(obj).forEach(function(key) {
  var t = (key + "->" + obj[key]);
});
pending…
Object prototype with for loop
var keys = Object.keys(obj),
    len = keys.length;
for (; i < len; i += 1) {
  var t = (keys[i] + "->" + obj[keys[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. Here’s a list of current revisions for this page:

1 comment

Tim (revision owner) commented :

Fixed the tests.

The returns in the previous revision completely break the test. As using Object.keys(obj) has longer setup time. but is more efficient when looking through large objects. At least in Chrome/v8.

Add a comment