JQuery each vs for loop vs _.each (lodash) (v21)

Revision 21 of this benchmark created by Derek Peterson on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
JQuery
function getArrayMin(array) {
  var min = -1;
  $.each(array, function(idx, val) {
    if (min < 0 && val > 0) {
      min = val;
      return true;
    }
    if (val > 0 && min > val) {
      min = val
      return true;
    }
  });
  return min;
};


getArrayMin([0, 10, 11, 9, 924, 19238, 1248, 8, 0, 0, 2, -1, -5]);
ready
forEach
function getArrayMin(array) {
  var min = -1;
  array.forEach(function ( val ) {
    if (min < 0 && val > 0) {
      min = val;
    } else if (val > 0 && min > val) {
      min = val;
    }
  });
  return min;
};


getArrayMin([0, 10, 11, 9, 924, 19238, 1248, 8, 0, 0, 2, -1, -5]);
ready
_.each
function getArrayMin ( array ) {
  var min = -1;
  _.each( array, function( idx, val ) {
    if ( min < 0 && val > 0 ) {
      min = val;
      return true;
    }
    if ( val > 0 && min > val ) {
      min = val;
      return true;
    }
  });
  return min;
};


getArrayMin([0, 10, 11, 9, 924, 19238, 1248, 8, 0, 0, 2, -1, -5]);
ready

Revisions

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