Closures

JavaScript performance comparison

Test case created by janjarfalk

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var names = ['zero','one','two','three','four','five','six','seven','eight','nine'];
   
    var digitNameGlobalVariable = function(n){
     return names[n];
    }
   
    var digitNamesPrivateVariable = function(n){
    var _names = ['zero','one','two','three','four','five','six','seven','eight','nine'];
    return _names[n];
    }
   
    var digitNamesClosure = (function(n){
    var _names = ['zero','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
Global Variable
digitNameGlobalVariable(3);
pending…
Private Variable
digitNamesPrivateVariable(3);
pending…
Closure
digitNamesClosure(3);
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