$.each vs. conversion to array and then loop

JavaScript performance comparison

Test case created by janmiksovsky

Preparation code

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

      
<script>
Benchmark.prototype.setup = function() {
  var s = "";
  for ( var i = 0; i < 100; i++ ) {
      s += "<div/>";
  }
  var $e = $( s );

};
</script>

Preparation code output

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
$.each()
$e.each( function( index, element ) {
    $( element ).text( index );
});
pending…
Convert to array and then loop
var a = [];
for ( var i = 0; i < $e.length; i++ ) {
    a.push( $( $e[i] ) );
}
for ( var i = 0; i < a.length; i++ ) {
    a[i].text( i );
}
pending…

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

Compare results of other browsers

0 Comments