Welcome Guest [Log In] [Register]
Search Members Calendar | Rules ZB Code Index IF Code Index
ZBCode
  • Navigation
  • ZBCode
  • Coding Support
  • Code University
  • Coding For Loops
Hey, welcome to ZBCode, the premier coding forum for ZB. Here you fill find some of the best Invisionfree and Zetaboards Codes on the network! Unfortunately, you're sorta hovering around in guest mode at the moment; why not join in on the fun? Register an account and you can start accessing the wealth of resources we have available here. Enjoy your stay at ZBCode, and remember to tell all your friends about us; the more members, the more codes available. ;)

Interested in joining? Click here.


If you are already a member of ZBCode, feel free to login right here:

Username:   Password:
Locked Topic
  • Pages:
  • 1
  • 2
Coding For Loops
Tweet Topic Started: Aug 15 2008, 07:24 AM (911 Views)
Webworldx Aug 15 2008, 07:24 AM Post #1


Posts:
36
Group:
Member
Member
#26
Joined:
Jul 14, 2008
Hi folks,

Just thought I'd throw in a quick one for all of you not converting to writing Javascript through libraries yet. Take a look at: http://www.openjs.com/articles/for_loop.php

I see a lot of people using something like:

Code:
 

var iTD = document.getElementsByTagName('TD');
for(i=0;i<iTD.length;i++){
...


.. i've been guilty of it myself previously, but think about the number of extra lookups going through the iterations - especially on say a TD or a DIV, or a SPAN.. that's 100's of elements per page.

:) Improve you efficiency
ZetaBoards Codes Index - 700+ ZB Codes

InvisionFree Codes Index - 6000+ Codes and Skins
Offline Profile Goto Top
 
Ryura Aug 15 2008, 08:06 AM Post #2
Member Avatar


Posts:
38
Group:
Member
Member
#11
Joined:
Jul 8, 2008
Coding language
Ruby on Rails
This *still* isn't the fastest way. For loops in general should be completely forgotten, actually - while loops are much faster.
This isn't actually necessary until you're looping arrays, specifically DOM compilations, with thousands of elements.

Code:
 

var LOOP = function(arr,funct) {
var _short= arr;
var _shortLength = _short.length;
var _shortLength2 = _shortLength;
do {
var i=_shortLength2-_shortLength;
funct(arr[i]); // in function, arr[i] represents current iteration
} while(--_shortLength);
}

Edited by Ryura, Aug 16 2008, 10:06 AM.
Offline Profile Goto Top
 
Webworldx Aug 15 2008, 09:45 AM Post #3


Posts:
36
Group:
Member
Member
#26
Joined:
Jul 14, 2008
Reverse while loops are indeed a good deal faster again, so a:

Code:
 

var i = iTD.length;
while(i--){

}


Might speed up your code even more.
ZetaBoards Codes Index - 700+ ZB Codes

InvisionFree Codes Index - 6000+ Codes and Skins
Offline Profile Goto Top
 
HolySavior Aug 15 2008, 10:13 AM Post #4
Member Avatar
Modifying The World Around You

Posts:
2,488
Group:
Distinguished Coder
Member
#7
Joined:
Jul 2, 2008
Coding language
Everything
nice. i like it. good find and thanks for posting that!
Offline Profile Goto Top
 
Godkillah Aug 15 2008, 10:32 AM Post #5


Posts:
55
Group:
Member
Member
#20
Joined:
Jul 13, 2008
as a while loop can do all a for loop can and even more for loops could easily be left out.
they just look so secksi
http://www.pdforums.net
For good and fast support on all your coding, graphics or skinning needs!
Offline Profile Goto Top
 
slayer766 Aug 15 2008, 11:36 AM Post #6
Member Avatar
Hello all

Posts:
1,653
Group:
Distinguished Coder
Member
#12
Joined:
Jul 9, 2008
Coding language
PHP
Yeah they do, and wow I never even thought of doing a for loop like this...
Offline Profile Goto Top
 
Ryura Aug 15 2008, 03:58 PM Post #7
Member Avatar


Posts:
38
Group:
Member
Member
#11
Joined:
Jul 8, 2008
Coding language
Ruby on Rails
Webworldx
Aug 15 2008, 09:45 AM
Reverse while loops are indeed a good deal faster again, so a:

Code:
 

var i = iTD.length;
while(i--){

}


Might speed up your code even more.

I'm *pretty* sure mine's still faster.I did a lot of research as well as my own testing a while back that lead me to my do...while loops.
Pre-decrementing is basically always faster than post (--i instead of i--). Obviously, even on huge arrays the difference between our methods is very low. After getting into decrementing a while or do..while loop, the only noticable speed increase you can make is to implement Duff's Device.
Edited by Ryura, Aug 15 2008, 04:24 PM.
Offline Profile Goto Top
 
Webworldx Aug 16 2008, 04:32 AM Post #8


Posts:
36
Group:
Member
Member
#26
Joined:
Jul 14, 2008
It will, but pre-dec only checks above zero, so the first element of the array gets missed.
Edited by Webworldx, Aug 16 2008, 04:33 AM.
ZetaBoards Codes Index - 700+ ZB Codes

InvisionFree Codes Index - 6000+ Codes and Skins
Offline Profile Goto Top
 
Ryura Aug 16 2008, 10:05 AM Post #9
Member Avatar


Posts:
38
Group:
Member
Member
#11
Joined:
Jul 8, 2008
Coding language
Ruby on Rails
Webworldx
Aug 16 2008, 04:32 AM
It will, but pre-dec only checks above zero, so the first element of the array gets missed.
Not in my loop.

Try this example:
Code:
 

var LOOP = function(arr,funct) {
var _short= arr;
var _shortLength = _short.length;
var _shortLength2 = _shortLength;
do {
var i=_shortLength2-_shortLength;
funct(i); // in function, arr[i] represents current iteration
} while(--_shortLength);
}
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
LOOP(arr,function(i){alert(arr[i])})
Offline Profile Goto Top
 
Zefer Aug 21 2008, 09:30 AM Post #10


Posts:
2
Group:
Member
Member
#681
Joined:
Aug 21, 2008
Coding language
Ruby on Rails
Code:
 

a=document.getElementsByTagName("div")
for(i in a)
{
// Code Here
};

That's how I do it.
Offline Profile Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Code University · Next Topic »
Locked Topic
  • Pages:
  • 1
  • 2

Track Topic · E-mail Topic Time: 7:48 PM Jul 10
Hosted for free by ZetaBoards · Privacy Policy