jQuery grep vs each (v9)

Revision 9 of this benchmark created on


Description

testing grep

Preparation HTML

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

<script>
  var arrToSearchThrough_large = [];
  var arrToSearchThrough_small = [{
   name: "7",
   res: "f"
  },
  {
   name: "6",
   res: "f"
  },
  {
   name: "4",
   res: "f"
  },
  {
   val: "9",
   res: "f"
  },
  {
   name: "112",
   res: "f"
  },
  {
   val: "abc",
   res: "f 1"
  },
  {
   val: "ddd",
   res: "f"
  },
  {
   name: "fff",
   res: "f"
  },
  {
   name: "abc",
   res: "f 2"
  },
  {
   name: "f",
   res: "f"
  },
  {
   name: "abc",
   res: "f 3"
  }];
  for (i = 0; i < 1000; i++)
  arrToSearchThrough_large.push(arrToSearchThrough_small);
  var arrResults = [];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
1 jQuery.grep
arrResults = $.grep(arrToSearchThrough_large, function(n, i) {
  return (n.name == "abc");
});
ready
2 jQuery.each
$(arrToSearchThrough_large).each(function() {
  if (this.name == "abc") arrResults.push(this);
});
ready
3 for-loop
for (i = 0; i < arrToSearchThrough_large.length; i++) {
  if (arrToSearchThrough_large[i].name == "abc") arrResults.push(arrToSearchThrough_large[i]);
}
ready
Filter
arrResults = $(arrToSearchThrough_large).filter(function(index) {
  return (this.name == "abc");
});
ready

Revisions

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