Nesteds: JQuery each vs native JS for loop

JavaScript performance comparison

Revision 37 of this test case created

Info

Testing the performance cost of nested loops: jQuery.each() vs. native JavaScript for loop

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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


getArrayMin([0, 10, 11, 9, 924, 19238, 1248, 8, 0, 0, 2, -1, -5]);
pending…
Native JavaScript for loop
function getArrayMin(array) {
  var min = -1;
  $.each(array, function(i, element){
    var val = array[i];
    if (min < 0 && val > 0) {
      min = val;
    }
    if (val > 0 && min > val) {
      min = val;
    }
    $.each(array, function(j){
      var val2 = array[j];
      if (min > 0 && val2 < 0) {
        min = val2;
      }
      if (val2 > 0 && min < val2) {
        min = val2;
      }
    });
  });
  return min;
};


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

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment