hasOwnProperty vs. for-in (v20)

Revision 20 of this benchmark created by runsun on


Description

Test performance hit of calling x.hasOwnProperty() in a for-in loop

Added 2013/7/25: Auto-run.

This page will run by itself (even though no "#run" is attached to the URL). Since the original design allows it to run with varying count values (by attaching '#cnnnn' to the url where nnnn is an integer), the ability of auto-run allows us to run and collect results of tests automatically.

Preparation HTML

<script>
  var MyObj = (function() {
   var i = -1,
       result = {},
       count = +location.hash.slice(2) || 50;
   while (++i < count) result[i] = 'x';
   document.getElementById('title-1').innerHTML += ' (' + count + ')';
   document.getElementById('title-2').innerHTML += ' (' + count + ')';
document.body.onload= function(){ 
   console.log('body loaded, ready to click [Run tests]');
   document.getElementById('run').click();
   console.log('clicked and run with count = '+ count)
}
   return result;

  })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
With hasOwnProperty
for (var i in MyObj) {
 if (MyObj.hasOwnProperty(i)) {}
}
ready
Without
for (var i in MyObj) {}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.