closure initialization

JavaScript performance comparison

Test case created by Ryan

Info

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

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];
        }
    }
   
};
</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…

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