editor icon indicating copy to clipboard operation
editor copied to clipboard

Detect infinite loops in JavaScript and stop execution

Open georgestagg opened this issue 4 years ago • 2 comments

For example, copying and pasting a for loop and editing the variables in order can lead to something like:

for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; i++) {
}
}

because the editor is saving changes as you type. This example causes an infinite loop it is hard to break from. Can this be situation detected and/or can some UI element help break from frozen scripts?

georgestagg avatar Sep 02 '20 07:09 georgestagg

Detecting infinite loops is famously an undecidable problem!

Codepen rewrites javascript to add a break check to every loop (https://blog.codepen.io/2017/03/23/infinite-loop-buster-round-three/). There's a standalone library called loop-protect which we could use.

christianp avatar Oct 05 '20 07:10 christianp

Welcome back! I guess asking you to solve the halting problem on your return is a little mean 😅

I was picturing something like a software watchdog timer but rewriting loops to inject breaks sounds like a very neat solution.

georgestagg avatar Oct 05 '20 07:10 georgestagg