full loop vs indices

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = function (i)
    {
    this.i = i;
    this.x = 0;
    this.dirty = false;
    };
   
    var objs = [];
    var someObjIndices = [0, 5, 20, 31, 42, 76, 98];
   
    for (var i = 0; i < 100; i++)
    {
    objs.push (new obj (i));
    }
   
    objs [0].dirty = true;
    objs [5].dirty = true;
    objs [20].dirty = true;
    objs [31].dirty = true;
    objs [42].dirty = true;
    objs [76].dirty = true;
    objs [98].dirty = true;
   
};
</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
full loop
for (var i = 0, l = objs.length; i < l; i++)
{
var obj = objs [i];
if (obj.dirty)
    obj.x += 1;
}
 
pending…
indices
for (var i = 0, l = someObjIndices.length; i < l; i++)
{
var obj = objs [someObjIndices [i]];
if (obj.dirty)
    obj.x += 1;
}
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