Object keys iteration

JavaScript performance comparison

Revision 32 of this test case created by Goktug

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 = Object.create({});
  var out = {};
 
  for (var i = 0; i < 10000; i++) {
   var key = 'user ' + i;
   data[key] =  key + 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) {
  if (key.indexOf("d") == 0) {
    out[key] = data[key];
  }
}
pending…
object-keys-foreach
Object.keys(data).forEach(function(key) {
  if (key.indexOf("d") == 0) {
    out[key] = data[key];
  }
});
pending…
object-keys
var keys = Object.keys(data);

for (var i = 0; i < keys.length; i++) {
  var key = keys[i];
  if (key.indexOf("d") == 0) {
    out[key] = data[key];
  }
}
pending…
object.getownpropertynames
var keys = Object.getOwnPropertyNames(data);

for (var i = 0; i < keys.length; i++) {
  var key = keys[i];
  if (key.indexOf("d") == 0) {
    out[key] = data[key];
  }
}
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