hasOwnProperty Performance Analysis

JavaScript performance comparison

Revision 7 of this test case created

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 = 8;
  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…

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