Object keys iteration (v28)

Revision 28 of this benchmark created by Mike on


Description

Testing Object.keys vs for-in. I've added jQuery's .each() for my own testing.

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var data = {};
  
  for (var i = 0; i < 5000; i++) {
  
   data[i] =  'user ' + i;
  
  }

  for (var j = i; j < 7500; j++) {
  
   data[j] = undefined;
  
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for-in
for (var key in data) {
  data[key];
}
ready
object-keys
Object.keys(data).forEach(function(element, index, array) {
  data[element];
});
ready
object-keys 2
var keys = Object.keys(data);
var len = keys.length;
var i = 0;

while (i < len) {
  data[keys[i]];
  i++;
}
ready
object-keys 3
var keys = Object.keys(data);
var len = keys.length;
var i;
var prop;

for (i = 0; i < len; i++) {
  data[keys[i]];
}
ready
$: [..].each() addressing using key
var keys = Object.keys(data);
keys.each(function(i) {
  data[keys[i]];
});
ready

Revisions

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