Loops

JavaScript performance comparison

Revision 105 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = new Array(100);
    var len = a.length;
   
    for(var i=-1;++i<len;) a[i]=i;
    var z = 99;
};
</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
for loop
for (var i = 0, n = a.length; i < n; ++i)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
for reverse loop
for (var i = a.length - 1; i >= 0; --i)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
for inc inline
for(var i = -1, n = a.length; ++i < n;)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
for reverse dec inline
for (var i = a.length; --i >= 0;)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
while reverse
var i = a.length - 1;
while (i > -1)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
    --i;
}
pending…
while reverse dec inline
var i = a.length;
while(i-- > 0)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
while reverse dec inline implicit
var i = a.length;
while(i--)
{
var test = a[i] * i;
    if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
for inside
for(var i in a){
var test = a[i] * i;
        if (typeof(a[i]) === "undefined")
        throw "error";
}
pending…
for outside
var i = null;
for(i in a){
var test = a[i] * i;
        if (typeof(a[i]) === "undefined")
        throw "error";
}
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