Object keys iteration (v114)

Revision 114 of this benchmark created 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 = {}, o = Object, Okeys = Object.keys; 
  
  for (var i = 0; i < 100; i++) {
  
   data[i] =  { foo: 'bar ' + i, bar: null };
  
  }
</script>

Test runner

Ready to run.

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

for (i = 0; i < len; ++i) {
  data[keys[i]];
}
ready
for loop (cached length + keys)
var keys = Object.keys(data),
  len = keys.length;

for (var i = 0, e; i < len, e = keys[i]; ++i) {
  data[e];
}
ready
while loop (cached keys + length) 1
var keys = Object.keys(data),
  len = keys.length,
  i = 0;

while (len) {
  data[keys[len]];
  len--;
}
ready
while loop (cached keys + length) 2
var keys = Object.keys(data),
  len = keys.length,
  i = 0;

while (i < len) {
  data[keys[i]];
  i++;
}
ready
while loop (cached keys + length) 3
var keys = Object.keys(data),
  len = keys.length,
  i = 0;

while (-1 < len) {
  data[keys[len]];
  len--;
}
ready
cached prop
var key;
for (key in data) {
data[key];
}
ready
cached object
var keys = o.keys(data),
  len = keys.length,
  i = 0;

while (i < len) {
  data[keys[i]];
  i++;
}
ready
cached outside fn
var keys = Okeys(data),
  len = keys.length,
  i = 0;

while (i < len) {
  data[keys[i]];
  i++;
}
ready
reverse while loop (cached keys)
var keys = Object.keys(data),
  i = keys.length;

while (--i) {
  data[keys[i]];
}
ready
shorter-object-keys
for (var keys = Object.keys(data), len = keys.length, i = 0; i < len; ++i) {
  data[keys[i]];
}
ready
AIO Object Keys
for (var keys = Object.keys(data), i = 0; i < keys.length; ++i) {
  data[keys[i]];
}
ready

Revisions

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