Loop Length and Var init
JavaScript performance comparison
Info
This is a simple bake off for checking loop speed with pre-initalized and cached vars vs more 'free form, on the fly loops'
Preparation code
<script>
var long_arr=new Array(100), item;
function cached_loop(arr) {
var lst=arr, l=lst.length, i=0;
for( ; i<l; i++) {
item=lst[i]
}
}
function uncached_loop(arr) {
for(var i=0; i<arr.length; i++) {
item=arr[i];
}
}
function shorthand_syntax_loop(arr) {
for(var i=0, l=arr.length; i<l; i++) {
item=arr[i]
}
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Local Variable Store |
|
pending… |
Check length Each Time |
|
pending… |
Shorthand Loop Syntax |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments