pcirella / dynatree

Automatically exported from code.google.com/p/dynatree
0 stars 0 forks source link

Optimize for loop .length! #159

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is not really a defect, more of a performance issue!

There a quite a few for loops in DynaTree like:

for(var i=0; i<this.childList.length; i++) {
 ...
}

I'd rather suggest caching the .length so this won't cause
expensive lookups, for every loop!

for(var i=0, l=this.childList.length; i<l; i++) {
 ...
}

I've changed all for loops in the latest trunk release (r366)
and all seems to be working and a bit smoother.

Original issue reported on code.google.com by prana001 on 17 Oct 2010 at 2:59

GoogleCodeExporter commented 9 years ago
could you send a patch file?

Original comment by moo...@wwwendt.de on 17 Oct 2010 at 8:22

GoogleCodeExporter commented 9 years ago
I've attached the modified version.

Original comment by prana001 on 17 Oct 2010 at 8:27

Attachments:

GoogleCodeExporter commented 9 years ago
Have you made any benchmarks or anything else that would detail 'working a bit 
smoother'?
(it would be easier to apply the changes, if you sent a patch file ('svn diff').

Anyway: thanks!

Original comment by moo...@wwwendt.de on 17 Oct 2010 at 8:50

GoogleCodeExporter commented 9 years ago
Benchmark results (IE 8)
   1.100000 x for() took 18 milliseconds (5555556 operations/sec)
   2.100000 x reverse-for() took 6 milliseconds (16666667 operations/sec)
   3.100000 x array iterator for() took 106 milliseconds (943396 operations/sec)
   4.100000 x array iterator cached-length took 70 milliseconds (1428571 operations/sec)
   5.100000 x array iterator reverse for() took 75 milliseconds (1333333 operations/sec)

Benchmark results (Firefox 3.6)
   1. 100000 x for() took 8 milliseconds (12500000 operations/sec)
   2. 100000 x reverse-for() took 4 milliseconds (25000000 operations/sec)
   3. 100000 x array iterator for() took 29 milliseconds (3448276 operations/sec)
   4. 100000 x array iterator cached-length took 22 milliseconds (4545455 operations/sec)
   5. 100000 x array iterator reverse for() took 20 milliseconds (5000000 operations/sec)

Benchmark results (Google Chrome 7)
   100000 x for() took 0 milliseconds
   100000 x reverse-for() took 0 milliseconds
   100000 x array iterator for() took 17 milliseconds (5882353 operations/sec)
   100000 x array iterator cached-length took 14 milliseconds (7142857 operations/sec)
   100000 x array iterator reverse for() took 15 milliseconds (6666667 operations/sec)

Test suite:
-----------   
test("Core benchmarks", function() {
    expect(5);

    var loopCount = 100000;
    benchmark(loopCount + " x for()", loopCount, function(){
        for(var i=0; i<loopCount; i++) {
            // no-op
        }
    });
    benchmark(loopCount + " x reverse-for()", loopCount, function(){
        for(var i=loopCount; i--; ) {
            // no-op
        }
    });
    var arr = new Array(loopCount);
    benchmark(loopCount + " x array iterator for()", loopCount, function(){
        for(var i=0; i<arr.length - 1; i++) {
            var e = arr[i]; 
            // no-op
        }
    });
    benchmark(loopCount + " x array iterator cached-length", loopCount, function(){
        for(var i=0, l=arr.length - 1; i<l; i++) {
            var e = arr[i]; 
            // no-op
        }
    });
    benchmark(loopCount + " x array iterator reverse for()", loopCount, function(){
        for(var i=arr.length - 1; i--; ) {
            var e = arr[i]; 
            // no-op
        }
    });
});

Original comment by moo...@wwwendt.de on 31 Oct 2010 at 11:22

GoogleCodeExporter commented 9 years ago
Yeah, Nice numbers!

Never knew a reverse-for can be almost twice as fast???!

Original comment by prana001 on 31 Oct 2010 at 11:33

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 21 Nov 2010 at 7:14

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 21 Nov 2010 at 7:26

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r439.

Original comment by moo...@wwwendt.de on 11 Dec 2010 at 7:46

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 12 Dec 2010 at 8:41

GoogleCodeExporter commented 9 years ago
considered verified

Original comment by moo...@wwwendt.de on 17 Jul 2012 at 4:16

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 17 Jul 2012 at 4:19