for-in vs underscore.each vs ldash.each (v2)

Revision 2 of this benchmark created by Prashant on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js">
</script>
<script>
  var underscore = _.noConflict();
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
</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
underscore.each
var total = 0;
underscore.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
lodash.each
var total = 0;
lodash.each(table, function (val, 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