Closures
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
Global Variable |
|
pending… |
Private Variable |
|
pending… |
Closure |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments