if vs function

JavaScript performance comparison

Test case created

Info

Tested whether if statement or function definition is faster than the other.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var text = '';
    var condition = false;
    var condfunction = condition ? function() {
        text += "firstcase";
      } : function() {
        text += "secondcase";
      }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
if statement
for (var a = 0; a < 1000; a++)
  if (condition) text += "firstcase";
  else text += "secondcase";
pending…
function use
for (var a = 0; a < 1000; a++)
  condfunction();
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