stackoverflow for vs. hasOwnProperty

JavaScript performance comparison

Revision 6 of this test case created by Joshua

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var mainList = [{
      id: "123456"
    }];
    var eArray = ["123456", "234567", "345678", "456789", "012345"];
    var eObject = {
      "123456": "0",
      "234567": "0",
      "345678": "0",
      "456789": "0",
      "012345": "0"
    };
};
</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
for (i = 0; i < mainList.length; i++) {
  for (j = 0; j < eArray.length; j++) {
    if (mainList[i]['id'] === eArray[j]) {
      //Do Something
    }
  }
}
pending…
hasOwnProperty
for (i = 0; i < mainList.length; i++) {
  if (eObject.hasOwnProperty(mainList[i]['id'])) {
    //Do Something
  }
}
pending…
if..in
for (i = 0; i < mainList.length; i++) {
  if (mainList[i]['id'] in eObject) {
    //Do Something
  }
}
pending…
for... in (no hasOwnProperty)
for (var i = 0; i < mainList.length; i++) {
  if (eObject[mainList[i].id] != undefined) {
    //do something
  }
}
pending…
for... in (no hasOwnProperty) storing key
var key;
for (var i = 0; i < mainList.length; i++) {
  key = mainList[i];
  if (eObject[key.id] !== undefined) {
    //do something
  }
}
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