Loop vs Sequence

JavaScript performance comparison

Test case created by RobG

Info

Test speed of a loop versus sequence of calls.

Preparation code

<script>
  function foo() {
    var x = Math.random() * Math.random();
    return x
  }
 
  // 100 iterations of 10 iterations of 1 call
  function loops() {
    var i = 100;
    var j = 10;
    var n;
    while (i--) {
      n = j;
      while (n--) {
        foo();
      }
    }
  }
 
  // 100 iterations of 10 calls
  function calls() {
    var i = 100;
    while (i--) {
      foo();
      foo();
      foo();
      foo();
      foo();
      foo();
      foo();
      foo();
      foo();
      foo();
    }
  }
 
  // 200 iterations of 5 calls
  function calls2() {
    var i = 200;
    while (i--) {
      foo();
      foo();
      foo();
      foo();
      foo();
    }
  }
 
  // 1,000 iterations of 1 call
  function justIterations() {
    var i = 1000;
    while (i--) foo();
   
  }
</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
Loops
loops();
pending…
Calls
calls();
pending…
justIterations
justIterations();
pending…
calls2
calls2();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment