block-scope-IIFE-vs-catch

JavaScript performance comparison

Test case created by Kyle Simpson

Info

Testing out perf diffs between using an IIFE to create some block scope, vs. using a forced-catch clause (ES3 block scope by spec).

Preparation code

<script>
function test1(a) {
   // IIFE is the usual way to do this in ES3
   (function(b){
      a += b;
   })(2);

   if (typeof b !== "undefined") throw "Test failed";
}

function test2(a) {
   // fake/transpiled ES3 emulation of `let (b=2) {...}`
   try{throw 2;}catch(b){
      a += b;
   }

   if (typeof b !== "undefined") throw "Test failed";
}

var seed;
</script>
<script>
Benchmark.prototype.setup = function() {
    seed = Math.random();
};
</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
IIFE
test1(seed);
pending…
catch-clause
test2(seed);
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