I have 2 solutions:
1.
My first solution would be to define a variable startSeconds=new Date().getSeconds(); .
Then, using regex, I insert this piece of code inside a nested loop.
;if(startSecond < new Date().getSeconds())break;
So, what does he do, every time the loop runs, he does two things:
Checks if startSecond is less than current seconds new Date().getSeconds(); .
For example, startSecond might be 22. new Date (). getSeconds () may return 24. Now, if condition succeeds, so it breaks the loop.
In general, a non-hazardous cycle should be performed within 2 to 3 seconds
Small loops, such as for(var i=0;i<30;i++){} , will run completely, but large loops will work for 3-4 seconds, which is quite normal.
My solution uses your own example 50,000 * 5000, but it does not crash!
Live demo: http://jsfiddle.net/nHqUj/4
2.
My second solution would be to define two variables start , max .
Max. there should be a maximum number of cycles that you are ready to run. Example 1000
Then, using regex, I insert this piece of code inside a nested loop.
;start+=1;if(start>max)break;
So, what does he do, every time the loop runs, he does two things:
This solution also uses your own example 50,000 * 5000, but it does not crash!
Updated demo: http://jsfiddle.net/nHqUj/3
Regex I use: (?:(for|while|do)\s*\([^\{\}]*\))\s*\{([^\{\}]+)\}