Own Prop Loop

JavaScript performance comparison

Revision 2 of this test case created

Info

Just wanted to look at differences for two types of object property looping when you only want to use an objects own properties and not the ones inherited from prototype.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = function() {
       return{ 'key': 'value', 'test': 'value', 'ownProp':'i hate prep work' };
    }
    obj.prototype.inheritKey = 'whats up';
    obj.prototype.wrench = 'thrown';
};
</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
hasOwnProperty()
var results = [];

for(var key in obj) {
  if(obj.hasOwnProperty(key)) {
    results.push([key, obj[key]]);
  }
}
pending…
getOwnProperties()
var results = [],
    ownProps = Object.getOwnPropertyNames(obj),
    i = 0, j = ownProps.length, key;

for(i; i<j; i++) {
  key = ownProps[i]
  results.push([key, obj[key]]);
}
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