hasOwnProperty Performance Analysis

JavaScript performance comparison

Revision 4 of this test case created and last updated

Info

This test is the same as revision 1 except that it does not add any extensions to the Object.prototype. To account for the fewer number of loops I've doubled the count var to 100

Preparation code

<script>
  var hasOwn = {}.hasOwnProperty;
  var count = +location.hash.slice(2) || 100;
  var randomInt = function (min, max) {
      return min + Math.floor(Math.random() * (max - min));
  }
  var randomString = function (len) {
      var s = '';
      while (len-- > 0) {
          s += String.fromCharCode(randomInt(65, 90));
      }
      return s;
  }
  var MyObj = (function() {
      var i = 0,
          result = {};
      while (i++ < count) result[randomString(randomInt(3, 40))] = i;
      return result;
  })();

</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
Using hasOwnProperty()
for (var key in MyObj) {
 if (MyObj.hasOwnProperty(key)) {
  var val = MyObj[key];
  var t = val.toString();
 }
}
pending…
Using static hasOwnProperty()
for (var key in MyObj) {
  if (hasOwn.call(MyObj, key)) {
    var val = MyObj[key];
    var t = val.toString();
  }
}
pending…
Raw
for (var key in MyObj) {
  var val = MyObj[key];
  var t = val.toString();
}
pending…
Object.keys().forEach()
Object.keys(MyObj).forEach(function (key) {
  var val = MyObj[key];
  var t = val.toString();
});
pending…
Object.keys() + loop
var keys = Object.keys(MyObj);
var length = keys.length;
for (var i = 0; i < length; i++) {
  var val = MyObj[keys[i]];
  var t = val.toString();
};
pending…
Object.getOwnPropertyNames().forEach()
Object.getOwnPropertyNames(MyObj).forEach(function (key) {
  var val = MyObj[key];
  var t = val.toString();
});
pending…
Object.getOwnPropertyNames() + loop
var keys = Object.getOwnPropertyNames(MyObj);
var length = keys.length;
for (var i = 0; i < length; i++) {
  var val = MyObj[keys[i]];
  var t = val.toString();
};
pending…
Object.keys().map() + loop
var values = Object.keys(MyObj).map(function(e) { return this[e] }, MyObj);
var length = values.length;
for (var i = 0; i < length; i++) {
  var val = values[i];
  var t = val.toString();
};
 
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