JQuery each vs for loop (v5)

Revision 5 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.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
for
function getArrayMin(array) {
  var min = -1;
  for (var i = 0; i < array.length; i += 1) {
    var val = array[i];
    if (min < 0 && val > 0) {
      min = val;
      continue;
    }
    if (val > 0 && min > val) {
      min = val
      continue;
    }
  }
  return min;
};


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


getArrayMin([0, 10, 11, 9, 924, 19238, 1248, 8, 0, 0, 2, -1, -5]);
ready
for cached
function getArrayMin(array) {
  var min = -1;
  var i = 0;
  var len = array.length;
  for (i; i < len; i++) {
    var val = array[i];
    if (min < 0 && val > 0) {
      min = val;
      continue;
    }
    if (val > 0 && min > val) {
      min = val
      continue;
    }
  }
  return min;
};


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


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


getArrayMin([0, 10, 11, 9, 924, 19238, 1248, 8, 0, 0, 2, -1, -5]);
ready
Inverted While
function getArrayMin(array) {
  var min = -1,
  i = array.length,
  val;
  while (i--) {
    val = array[i];
    if (min < 0 && val > 0) {
      min = val;
      continue;
    }
    if (val > 0 && min > val) {
      min = val
      continue;
    }
  }
  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.