views:

120

answers:

6

I've been having a crack at some of the problems over at http://projecteuler.net/ with JavaScript. I've been using a simple html page and running my code in script tags so I can log my results in the browsers' console. When experimenting with loops I sometimes cause the browser to crash.

Is there a better environment for me to do this kind of development?

+4  A: 
  1. a browser that has separate processes for each tab
  2. debugger breakpoints
  3. an if that breaks the loop if some threshold for time is hit
BioBuckyBall
A: 

There's nothing you can do to keep the browser from crashing other than fix bugs that cause the browser to crash.

You can at least mitigate the impact of the crash by using a browser like Chrome that generally segregates crashes in one tab from the others (so you lose only your own page), or just installing a separate browser specifically for testing.

In terms of keeping track of data that might have gone to the log, you might use a plugin like Firebug that has a built-in debugger so you can pause the script execution partway through and examine your variables, presumably before any crash occurs.

VoteyDisciple
+1  A: 

If you're running computationally expensive programs in your browser, you may want to look at using web workers. In short, they allow you to run code in a different thread which won't lock up the browser.

nickf
+1  A: 

If you are just interested in running javascript programs as such, why don't you use something like node.js or even Rhino? That way you can easily log output without loosing it if it get into 'trouble'.

unomi
+1  A: 

I can think of two ready possibilities:

1) Use a debugger that has breakpoints. Firebug is rather nice. Safari and Chrome also have some built-in debugging tools.

2) You could move your testing out of the browser using Mozilla Rhino and Env-js (see http://groups.google.com/group/envjs and http://github.com/thatcher/env-js )

Weston C
+1  A: 

All modern browsers (except Opera) should interrupt your script if it runs for more than 5-10 seconds (Source).

In Firefox you can even lower this threshold, if 10 seconds mean a too big punishment. Also note that this mechanism kicks in even when you run code from the Firebug console:

Stop Script on Firefox

I think this feature alone should provide a pretty safe environment for these loopy experiments :)

Daniel Vassallo
When I get the screenshot above and I press, "stop script", firefox can show as "not responding" and freezes. To quote you "All modern browsers (except Opera) should interrupt your script" - "should" is important here - If they **did** then there would be no reason for me posting this question. Maybe this is due to my environment (addons, os etc.) or the intracacies of my experiments (less obvious to the browser/console than `while(true)`).
Dr. Frankenstein
@yaya3: Yes that's why I used "should", as I guessed this is not happening for you. By default, the mechanism should kick in whenever the script is running for 10 seconds. There is no need to have an infinite loop. JavaScript inside browsers was never intended for long running processing, since it runs in the single UI thread, and blocks UI updates completely... You may want to check the [`about:config`](http://kb.mozillazine.org/Dom.max_script_run_time) to see if this is disabled in your Firefox. Else you may want to use another browser for the Project Euler experiments.
Daniel Vassallo
@Daniel Ah right, apologies, I misunderstood your answer. It's interesting looking at these browser variables. I've found reducing the `dom.max_script_run_time` has speeded up my testing in FF - cheers.
Dr. Frankenstein