Object keys iteration (v61)

Revision 61 of this benchmark created by readyyyyy 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: 'bar ' + i, bar: null };
  
  }
</script>

Setup

function forEach(d, fn) {
      var keys = Object.keys(d);
      var len = keys.length;
      var i;
    
      for (i = 0; i < len; ++i) {
        fn(d[keys[i]]);
      }
    }

Test runner

Ready to run.

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

for (i = 0; i < len; ++i) {
  a = data[keys[i]];
}
ready
object-keys 4
var a;
forEach(data, function(value) {
  a = value;
});
ready

Revisions

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