for-in vs underscore.each

Benchmark created by Prashant on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>

Setup

var table = {};
    var i;
    for (i = 0; i < 20; i += 1) {
      table['foo' + i] = 'bar' + (i+5);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
for-in
var val;
var total = 0;
for (key in table) {
  if (table.hasOwnProperty(key)) {
    val = table[key];
    total += val.length + key.length;
  }
}
 
ready
_.each
var total = 0;
_.each(table, function (val, key) {
  total += val.length + key.length;
});
 
ready
Object.keys
var total = 0;
Object.keys(table).forEach(function (key) {
  var val = table[key];
  total += val.length + key.length;
});
ready

Revisions

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

  • Revision 1: published by Prashant on
  • Revision 2: published by Prashant on