Early return or not

JavaScript performance comparison

Test case created by Chris Khoo

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function once() {
      var done = false;
      return function() {
        if (done) {
          return undefined;
        } else {
          done = true;
          return 1;
        }
      }
    }
   
    function onceEarlyReturn() {
      var done = false;
      return function() {
        if (done) return undefined;
        done = true;
        return 1;
      }
    }
};
</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
once
once();
once();
once();
once();
once();
once();
once();
pending…
onceEarlyReturn
onceEarlyReturn();
onceEarlyReturn();
onceEarlyReturn();
onceEarlyReturn();
onceEarlyReturn();
onceEarlyReturn();
onceEarlyReturn();
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