Loop vs Sequence

Benchmark created by RobG on


Description

Test speed of a loop versus sequence of calls.

Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
Loops
loops();
ready
Calls
calls();
ready
justIterations
justIterations();
ready
calls2
calls2();
ready

Revisions

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