Dr. Frankenstein
Dr. Frankenstein

Reputation: 4704

How can I test potentially "browser-crashing" JavaScript?

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?

Upvotes: 8

Views: 4694

Answers (6)

Daniel Vassallo
Daniel Vassallo

Reputation: 344441

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 http://img819.imageshack.us/img819/9655/infloopsp.jpg

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

Upvotes: 1

Weston C
Weston C

Reputation: 3632

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 )

Upvotes: 1

unomi
unomi

Reputation: 2672

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'.

Upvotes: 1

nickf
nickf

Reputation: 546273

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.

Upvotes: 1

VoteyDisciple
VoteyDisciple

Reputation: 37813

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.

Upvotes: 0

µBio
µBio

Reputation: 10758

  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

Upvotes: 5

Related Questions