underscore each vs for vs for in loops (v13)

Revision 13 of this benchmark created by Dan on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://documentcloud.github.com/underscore/underscore.js"></script>
<div id="output"></div>

Setup

var $ = window.jQuery,
      _ = window._,
      obj = {"test" : 123, "best" : 123, "rest" : 123	},
      i,j,
      len = Object.keys(obj).length,
      outputDiv = document.getElementById("output"),
      keys = Object.keys(obj);

Test runner

Ready to run.

Testing in
TestOps/sec
Current Implementation
_.each(obj, function(o){
 var a = o.test,
     b = o.best,
     c = o.rest;
});
ready
for loop
for(i= 0; i < len;i++){
 var a = obj[keys[i]].test,
     b = obj[keys[i]].best,
     c = obj[keys[i]].rest;
};
ready
for in loop
for(j in obj){
 var a = obj[j].test,
     b = obj[j].best,
     c = obj[j].rest;
};
ready

Revisions

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

  • Revision 1: published by Jeremy Ashkenas on
  • Revision 4: published by Paul Miller on
  • Revision 5: published by John-David Dalton on
  • Revision 7: published by Shane O'Sullivan on
  • Revision 9: published on
  • Revision 10: published on
  • Revision 11: published by Jesús Germade on
  • Revision 12: published by test on
  • Revision 13: published by Dan on
  • Revision 14: published by Jesús Germade on
  • Revision 15: published by Jesús Germade on
  • Revision 16: published by Jesús Germade on
  • Revision 17: published by mingc on
  • Revision 18: published by Eldar Abusalimov on