Object keys iteration

JavaScript performance comparison

Revision 41 of this test case created

Info

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

Preparation code

<script>
  var data = {};
 
  for (var i = 0; i < 10007; i++) {
 
   data[i] =  'user ' + i;
 
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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

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

for (i = 0; i < len; i++) {
  data[keys[i]];
}
pending…
object-keys unrolled
var keys = Object.keys(data);
var len = keys.length;
var len8 = len - 8;
var i;

for (i = 0; i < len8; i += 8) {
  data[keys[i]];
  data[keys[i + 1]];
  data[keys[i + 2]];
  data[keys[i + 3]];
  data[keys[i + 4]];
  data[keys[i + 5]];
  data[keys[i + 6]];
  data[keys[i + 7]];
}
if (len & 4) {
  data[keys[i]];
  data[keys[i + 1]];
  data[keys[i + 2]];
  data[keys[i + 3]];
  i += 4;
}
if (len & 2) {
  data[keys[i]];
  data[keys[i + 1]];
  i += 2;
}
if (len & 1) {
  data[keys[i]];
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment