My favorites | Sign in
Project Home Downloads
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 159: Optimize for loop .length!
1 person starred this issue and may be notified of changes. Back to list
Status:  Verified
Owner:  ----
Closed:  Dec 2010


 
Reported by prana001, Oct 17, 2010
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
Project Member #1 moo...@wwwendt.de
could you send a patch file?
Status: Waiting
Labels: -Type-Defect Type-Enhancement Milestone-Release1.0
Oct 17, 2010
#2 prana001
I've attached the modified version.
dynatree.min.js
174 KB   View   Download
Oct 17, 2010
Project Member #3 moo...@wwwendt.de
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
Project Member #4 moo...@wwwendt.de
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
#5 prana001
Yeah, Nice numbers!

Never knew a reverse-for can be almost twice as fast???!
Nov 20, 2010
Project Member #6 moo...@wwwendt.de
(No comment was entered for this change.)
Status: Accepted
Labels: -Priority-Medium Priority-Low
Nov 20, 2010
Project Member #7 moo...@wwwendt.de
(No comment was entered for this change.)
Labels: Milestone-Release1.1
Dec 10, 2010
Project Member #8 moo...@wwwendt.de
This issue was closed by revision r439.
Status: Fixed
Dec 12, 2010
Project Member #9 moo...@wwwendt.de
(No comment was entered for this change.)
Labels: Milestone-Release1.0.3
Jul 17, 2012
Project Member #10 moo...@wwwendt.de
considered verified
Jul 17, 2012
Project Member #11 moo...@wwwendt.de
(No comment was entered for this change.)
Status: Verified

Powered by Google Project Hosting