jQuery.each vs. for loop

JavaScript performance comparison

Test case created by Joe Clay

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  Catalog = {
  cdata: [],
  cfound: [],
  }
  
  for(var i = 0; i < 1000; i++) {
  		Catalog.cdata[i] = 'aaaaaaaaa';
  }
  
  var len = Catalog.cdata.length;

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for
for(var i = 0; i < len; i++) {
	if(Catalog.cdata[i].toLowerCase().search('a')!==-1) {
		Catalog.cfound.push(Catalog.cdata[i]);
	}
}
pending…
jQuery.each
$.each(Catalog.cdata, function(index, data) {
	if(data.toLowerCase().search('a')!==-1) {
		Catalog.cfound.push(data);
	}
});
pending…

Revisions

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

0 Comments