| Issue 159: | Optimize for loop .length! | |
| 1 person starred this issue and may be notified of changes. | Back to list |
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.
Oct 17, 2010
I've attached the modified version.
Oct 17, 2010
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!
Oct 31, 2010
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
}
});
});
Oct 31, 2010
Yeah, Nice numbers! Never knew a reverse-for can be almost twice as fast???!
Nov 20, 2010
(No comment was entered for this change.)
Status:
Accepted
Labels: -Priority-Medium Priority-Low
Nov 20, 2010
(No comment was entered for this change.)
Labels:
Milestone-Release1.1
Dec 12, 2010
(No comment was entered for this change.)
Labels:
Milestone-Release1.0.3
Jul 17, 2012
considered verified
Jul 17, 2012
(No comment was entered for this change.)
Status:
Verified
|
Labels: -Type-Defect Type-Enhancement Milestone-Release1.0