Global function vs. local function

JavaScript performance comparison

Test case created by Ted Hopp

Info

Compares calling a global function with calling a local function. Motivated by the SO question at

http://stackoverflow.com/questions/14407099/why-those-javascript-snippets-perform-the-same

Modified to pass parameter to explicit iter() function and to eliminate function body (to better measure function call alone).

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    obj = [1, 2, 3, 4, 5];
   
    function iter(obj) {
      for (var key in obj) {
        // key = key + key;
      };
    };
   
    function test1() {
      iter(obj);
    };
   
    function test2() {
      (function (obj) {
        for (var key in obj) {
          // key = key + key;
        };
      })(obj);
    };
};
</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
Explicit function
test1();
pending…
nested function
test2();
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

0 comments

Add a comment