closure initialization

JavaScript performance comparison

Revision 3 of this test case created by tomByrer

Info

Testing if using a closure to avoid reinitializing an array every function call is any faster.

v3: v2 programed varoutsidescope() but not called

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var digit_name = function(n) {
        var names = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
        return names[n];
    }
   
    var digit_name_closure = ( function() {
        var names = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
        return function(n) { return names[n]; }
    }());
   
    var n_out = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
    var var_outside_scope = function(n) { return n_out[n]; };
};
</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
no-closure
digit_name(3)
pending…
closure
digit_name_closure(3)
 
pending…
var outside
var_outside_scope(3)
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