views:

118

answers:

3

Hi all,

I have recently started to tinker with Project Euler problems and I try to solve them in Javascript. Doing this I tend to produce many endless loops, and now I'm wondering if there is any better way to terminate the script than killing the tab in Firefox or Chrome?

Also, is firebug still considered the "best" debugger (myself I can't see much difference between firebug and web dev tool in safari/chrome ).

Any how have a nice Sunday!

+1  A: 

Firebug is still my personal tool of choice.

As for a way of killing your endless loops. Some browsers will prevent this from happening altogether. However, I still prefer just going ctrl + w, but this still closes the tab.

Some of the other alternatives you can look into:

Opera : Dragonfly

Safari / Chrome : Web Inspector

Although, Opera has a nice set of developer tools which I have found pretty useful. (Tools->Advanced->Developer Tools)

Russell Dias
I primarily use either Firefox or Safari, but the developer tools in IE are surprisingly good, though you still need to use Fiddler to watch for file requests. I wouldn't use IE for any JS where execution time is an issue like this, but for doing general web debugging, it's quite good.
Andrew
A: 

If you don't want to put in code to explicitly exit, try using a conditional breakpoint. If you open Firebug's script console and right-click in the gutter next to the code, it will insert a breakpoint and offer you an option to trigger the breakpoint meets some condition. For example, if your code were this:

var intMaxIterations = 10000;
var go = function() {
    while(intMaxInterations > 0) {
        /*DO SOMETHING*/
        intMaxIterations--;
    }
};

... you could either wait for all 10,000 iterations of the loop to finish, or you could put a conditional breakpoint somewhere inside the loop and specify the condition intMaxIterations < 9000. This will allow the code inside the loop to run 1000 times (well, actually 1001 times). At that point, if you wish, you can refresh the page.

But once the script goes into an endless loop (either by mistake or design), there's not a lot you can do that I know of to stop it from continuing if you haven't prepared for this. That's usually why when I'm doing anything heavily recursive, I'll place a limit to the number of times a specific block of code can be run. There are lots of ways to do this. If you consider the behaviour to be an actual error, consider throwing it. E.g.

var intMaxIterations = 10000;
var go = function() {
    while(true) {
        /*DO SOMETHING*/
        intMaxIterations--;

        if (intMaxIterations < 0) {
            throw "Too many iterations. Halting";
        }
    }
};

Edit: It just occurred to me that because you are the only person using this script, web workers are the ideal solution.

The basic problem you're seeing is that when JS goes into an endless loop, it blocks the browser, leaving it unresponsive to any events that you would normally use to stop the execution. Web workers are still just as fast, but they leave your browser unburdened and events fire normally. The idea is that you pass off your high-demand tasks (in this case, your Euler problem algorithm) to a web worker JS file, which executes in its own thread and consumes CPU resources only when they are not needed by the main browser. The net result is that your CPU still spikes like it does now, but your browser stays fast and responsive.

It is a bit of a pest setting up a web worker the first time, but in this case you only have to do it once. If your algorithm never returns, just hit a button and kill the worker thread. See Using Web Workers on MDC for more info.

Andrew
Ok guess I have to learn not to discover new ways to do endless loops it seems ;).
Buzzzz
Well, another thing you can do is create a keyboard event that sets a value that your code looks for. When that condition is met, you can exit or throw an exception. The problem with this is that often the keyboard callback is so far down in the queue that it's often faster to kill the browser.
Andrew
Thanks for accepting my answer. I've added a note you may want to read about using web workers. This may be just what you need. Plus it's pretty cutting-edge, which is always cool.
Andrew
A: 

While having Firebug or the webkit debuggers is nice, a browser otherwise seems like overhead for Project Euler stuff. Why not use a runtime like Rhino or V8?

Weston C
Mm but I really like a debugger and I guess that node.js doesnt have that ( or is it possible to run V8 without node.js outside a browser) ?
Buzzzz