[Project_owners] getElementsByTagName() problem

HJ bugs4hj at yahoo.com
Thu Apr 13 16:07:48 EDT 2006


Eric H. Jung wrote:
> Hi,
> 
>>>> The problem with recursive JS functions is that the end user most likely 
>>>> ends up with a sluggish feeling browser.
>>> How is recursion any less performant or more sluggish than repeatedly calling a function
>> within a
>>> loop (as in your code)?
>> What?  You've never seen the "Too many recursions" errors?
>> Also, recursions stacks up quite a bit, and that leads to a sluggish 
>> feeling browser.
>>
> 
> Yes, I have seen that error. For example:
> 
> (function() {arguments.callee();})();
> 
> It is my understanding "InternalError: too much recursion" is no different than a stack overflow
> error. For instance:
> 
> // Create a long chain of functions and call
> // the first in the chain
> for (var i=0; i<9999; i++)
>   eval("function a" + i + "(){ a" + (i+1) + "();}");
> a0();
> 
> This also yields "InternalError: too much recursion" even though there is no recursion. A more
> appropriate error description, IMHO, would be "Stack overflow error" or "No more stack space
> available" because recursion is not necessary to overflow the stack.
> 
> So my question is, "Why would recursion be any less efficient (or more visible to the user as
> sluggishness) than a function chain of the same depth?"

Because like I said, it stacks up more, uses more memory and is harder 
to debug, not to mention that your version will be up to 180ms slower 
than my example.

Recursion should (IMHO) only be used if it can't be don't other wise,
if it saves you lines (can you make a smaller working example?) because 
recursion can reduce the number of JS lines in specific cases.  However, 
you already had duplicated code in your example so that shouldn't be a 
valid argument ;)

>> Now I seriously need a cup of coffee and a proper breakfast!
> Have some for me, too :)

I did, but there is no StarBucks here so I had to make some myself
FYI: there is StarBucks in Afghanistan: 
http://www.warriorswithwrenches.com/star.html but not in the low 
countries.  Man what a joke!

/HJ


More information about the Project_owners mailing list