Object keys iteration (v73)

Revision 73 of this benchmark created by Jerry on


Description

Testing Object.keys vs for-in. I see that SlickGrid uses objects with indexed keys rather than arrays. Confused....

Preparation HTML

<script>
  var data = {};
  
  for (var i = 0; i < 100; i++) {
  
   data[i] =  { foo:i, bar: null };
  
  }

function someFn(ix) {
    try { 
        ix += 1; 
        if (ix < 0) {
           throw new Error('e');
        }
    } catch (e){
        alert(e);
    }
    return ix * 5 + 1 / 3 * 8;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for-in
for (var key in data) {
  if (Object.prototype.hasOwnProperty.call(data, key)) {
    someFn(data[key]);
  }
}
ready
for-in-cached-hasOwnProperty
var ownProp = Object.prototype.hasOwnProperty;
for (var key in data) {
  if (ownProp.call(data, key)) {
    someFn(data[key]);
  }
}
ready
for-in-no-hasOwnProperty
for (var key in data) {
  someFn(data[key]);
}
ready
object-keys 3
var keys = Object.keys(data);
var len = keys.length;
var i;

for (i = 0; i < len; ++i) {
  someFn(data[keys[i]]);
}
ready
object-keys 4
var keys = Object.keys(data);
var len = keys.length;
var i;

while(i = keys.shift()){
  someFn(data[i]);
}
 
ready

Revisions

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