Object keys iteration

JavaScript performance comparison

Revision 5 of this test case created by Michael Jostmeyer

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 < 10000; i++) {
 
   data['xyz_'+i] =  'user ' + i;
   data['abc_'+i] = function(){};
 
  }
</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),
    len = keys.length;

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

for (; i < len;) {
  data[keys[i++]];
}
pending…
for-in (filtered)
for (var key in data) {
   if(data.hasOwnProperty(key)){
      data[key];
   }
}
pending…
for-in without functions 1
var key, value;
for (key in data) {
   value = data[key];
   if(typeof value !== 'function' && data.hasOwnProperty(key)){
      value;
   }
}
pending…
for-in without functions 2
var key, value;
for (key in data) {
   value = data[key];
   if(typeof value !== 'function'){
      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