开发者

Javascript debugging

开发者 https://www.devze.com 2023-01-16 14:02 出处:网络
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 th开发者_开发知识库ere i

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 th开发者_开发知识库ere 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!


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)


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消