Cost of creating inner function

JavaScript performance comparison

Test case created by T.J. Crowder

Info

Looks at the cost of creating an inner function

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function fooWithBar(a, b) {
        function bar(x) {
            return x * b;
        }
   
        if (a < 3) {
            return bar(a * 2);
        }
        return bar(a * 3);
    }
   
    function fooWithoutBar(a, b) {
        if (a < 3) {
            return (a * 2) * b;
        }
        return bar(a * 3) * b;
    }
   
};
</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
With a function
if (fooWithBar(2, 3) !== 12) throw "Error in test";
pending…
Without function
if (fooWithoutBar(2, 3) !== 12) throw "Error in test";
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