Object keys iteration

JavaScript performance comparison

Revision 45 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 Map = function() {
        this._dirty = true;
        this._map = {};
        this._keys = undefined;
    };

    Map.prototype = {
        dirty: function() { this._dirty = true; },
        setMap: function( map ) { this._map = map; this._dirty = true; },
        getMap: function() { return this._map; },
        getKeys: function() {
            if ( this._dirty ) {
                this._keys = Object.keys( this._map );
            }
            return this._keys;
        }
    };
 
  var data = [];
 
  for (var i = 0; i < 100; i++) {
 
   data['o'+i] =  { foo: 'bar ' + i, bar: null };
 
  }
  var map = new Map( data );
</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 (Object.prototype.hasOwnProperty.call(data, key)) {
    data[key];
  }
}
pending…
for-in-cached-hasOwnProperty
var ownProp = Object.prototype.hasOwnProperty;
for (var key in data) {
  if (ownProp.call(data, key)) {
    data[key];
  }
}
pending…
for-in-no-hasOwnProperty
for (var key in data) {
  data[key];
}
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 4
var keys = map.getKeys();
var content = map.getMap();
var len = keys.length;
var i;

for (i = 0; i < len; ++i) {
  content[keys[i]];
}
pending…
foreach
data.forEach(function(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