Conditionals and Loops

JavaScript performance comparison

Test case created by AthanClark

Info

This test checks to see if when a conditional is supplied as a parameter to a function, and a loop is made inside the function, whether or not it is more efficient to evaluate a conditional once and create two non-interfereing loops rather than to have the conditional evaluated at each loop run.

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Internal conditional
var bull = { val: 0 };
(function( loops, evaluation, bs ) {
  for ( var i = 0; i < loops; ++i ) {
    if ( evaluation ) {
      bs.val += 1;
    } else {
      bs.val -= 1;
    }
  }
})( 100, true, bull );
pending…
External conditional
var bull = { val: 0 };
(function( loops, evaluation, bs ) {
  if ( evaluation ) {
    for ( var i = 0; i < loops; ++i ) {
      bs.val += 1;
    }
  } else {
    for ( var i = 0; i < loops; ++i ) {
      bs.val -= 1;
    }
  }
})( 100, true, bull );
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

Add a comment