Loop for…in vs Object.keys.forEach vs lodash.forIn (long keys) (v14)

Revision 14 of this benchmark created on


Description

Double checking the additional hasOwnProperty check so that the tests are equivalent

Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js">
</script>
<script>
var obj = 
{
},
i = 0,
keys = ["loremFistrumQue", "diseUsteerTe", "vaAHasePupita", "deLa", "praderaCaballoBlanco", "caballoNegroorlTe", "teVoyABorrarelCerito", "benemeritaarFistrodela", "praderaAlAtaquerldeLapradera", "tieneMushoPeligro", "aPeichDiodenoo", "caballoBlanco", "caballoNegroorl", "seCalleUstee"]
</script>

Setup

i = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
for…in
for (key in obj) {
  if (obj.hasOwnProperty(key)) {
    i += obj[key];
  }
}
ready
Object.keys
Object.keys(obj).forEach(function(key) {
  i += obj[key];
});
ready
Predefined array of keys
for (var j = 0, keys = Object.keys(obj), len = keys.length; j < len; j++) {
  var key = keys[j];
  i += obj[key];
}
ready
for..in no hasOwnProperty
for (key in keys) {
  i += obj[key];
}
ready
lodash forOwn
_.forOwn(obj, function(val) {
  i += val;
});
ready
lodash forIn
_.forIn(obj, function(val) {
  i += val;
});
ready
lodash forEach
_.forEach(obj, function(val) {
  i += val;
});
ready
for in (no hasprop)
for (key in obj) {

    i += obj[key];

}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.