objdir

JavaScript performance comparison

Revision 6 of this test case created by Alexander Kolarski

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var create = function(count) {
   
        "use strict";
       
        function Hash() {}
   
        var i = 0, half = count/2, proto = {}, h;
   
        for (; i < half; i += 1) {
          proto['' + i] = i;
        }
       
        Hash.prototype = proto;    
   
        h = new Hash();
   
       // only half will be own properties
        for (; i < count; i += 1) {
          h[''+i] = i + 1;
        }
   
        return h;
   
    },
   
    obj = create(10000);
};

Benchmark.prototype.teardown = function() {
    obj = null;
};
</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 without hasOwnProperty
for (var a in obj) {
  return (a + "->" + obj[a]);
}
pending…
cached for-in without hasOwnProperty
var a;
for (a in obj) {
  return (a + "->" + obj[a]);
}
pending…
Object prototype (with implicit hasOwnProperty)
Object.keys(obj).forEach(function(key) {
  return (key + "->" + obj[key]);
});
pending…
Object prototype with for loop (with implicit hasOwnProperty)
var i, keys = Object.keys(obj), len = keys.length;

for (; i < len; i += 1) {
  return (keys[i] + "->" + obj[keys[i]]);
}
pending…
cached for-in loop with hasOwnProperty
var a, owns = Object.prototype.hasOwnProperty;
for (a in obj) {
  if (owns.call(obj, a)) {
    return (a + "->" + obj[a]);
  }
}
pending…
for-in with hasOwnProperty
for (var a in obj) {
  if (obj.hasOwnProperty(a)) {
    return (a + "->" + obj[a]);
  }
}
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