IIFE vs. simple function

JavaScript performance comparison

Test case created by Chris Khoo

Info

By "simple", I mean simple to understand.

If you know of a better term, please let me know!

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var iife = (function() {
      function helper(arg) {
        return arg + 1;
      }
   
      return function(arg1, arg2) {
        return helper(arg1) + helper(arg2);
      }
    })();
   
    function simple(arg1, arg2) {
      function helper(arg) {
        return arg + 1;
      }
   
      return helper(arg1) + helper(arg2);
    }
};
</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
iife(1, 2);
pending…
Simple
simple(1, 2);
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

1 comment

Chris Khoo (revision owner) commented :

BTW, the reason IIFE is faster is because it doesn't need to re-declare the "helper" function every time it runs.

Add a comment