Object keys iteration

JavaScript performance comparison

Revision 35 of this test case created by gfgfg

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 = {
1 : 1,
2 : 1,
5 : 1,
10 : 1,
11 : 1,
15 : 1
};
  var out = {};
</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) {
  out[key] = data[key];
}
pending…
object-keys-foreach
Object.keys(data).forEach(function(key) {
  out[key] = data[key];
});
pending…
object-keys
var keys = Object.keys(data);

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

for (var i = 0; i < keys.length; i++) {
  var key = keys[i];
  out[key] = data[key];
}
pending…
array iteration
for (var i = 0; i < 20; i++) {
  if (data[i]) {
    out[i] = data[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