Object keys iteration

JavaScript performance comparison

Revision 50 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 src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
  var data = {};
 
  for (var i = 0; i < 10000; i++) {
 
   data[i] = {
      name: 'user ' + i,
      test: 'test ' + i,
      info: 'info ' + i
   };
 
  }
</script>

Preparation code output

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[i];
  i++;
}
pending…
object-keys 3
var keys = Object.keys(data);
var len = keys.length;
var i;

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

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

for (; --i;) {
  data[i];
}
pending…
$.each
$.each(data, function(key, value) {
  value;
});
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