editor
editor copied to clipboard
Detect infinite loops in JavaScript and stop execution
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?
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.
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.