[Project_owners] intensive recursions in Javascript

Andrey Gromyko andrey at gromyko.name
Mon May 7 14:44:47 PDT 2007


Hi Preet,

On Mon, 7 May 2007 11:21:12 -0700
"Preet Shihn" <preetshihn at gmail.com> wrote:

> Heyas!
> 
> I am a semi-javascript newbie. I noticed that Javascript isn't really
> great with long running recursive methods or a long running while
> loop that can be memory intensive.
> I see the browser popup "A script on the page may be busy, or it may
> have stopped responding..... Stop the script or continue". I was
> wondering how people have dealt with this. An example of such a
> method that I was implementing is Flood Fill. Basically, fill a
> contoor with a specified colour.
> 
> I know you can set a preference so that this message does not popup
> untill you run a bazillion line (whatever you set to), but that is
> not what I am looking for. I want a better performance.
> 
> Other alternatives I can think of is wrting a custom xpcom object in
> C that does my computation.. or maybe a hidden java applet.
> 
> It would be good to know how other people have dealt with this
> problem in general.

In general, it is better to avoid recursion. In most cases you can substitute it with loops. Have you tried that?
Most popular scholar example is factorial calculation. You may write two functions for that in C - one using recursion, another for-loop - and try to run it on some significant number timestamping begin and end of a calculation. You should see the difference ;)

Cheers,
Andrey

> Thanks,
> Preet
> 
> p.s. In case anyone is interested, my flood fill method is a modified
> version of http://www.htmlgoodies.com/primers/jsp/article.php/3622321
> 


More information about the Project_owners mailing list