Re: Javascript recursion limit
On May 17, 11:44 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
Look closer.
>
No, it is the same number if I use
>
function f(x)
{
console.log(x);
f(x + 1);
}
>
f(0);
because there are still two stack position used: for f(0) call and for
f(x) for the initial enter. The only difference from the first option
is that here the first stack position is taken by a named function and
in the first example - by an anonymous function. Sorry, but you really
should refresh the stack mechanics theory during this week-end.
>
It is the same number when using
>
javascript: function f(x) { console.log(x); f(x + 1); } f(0);
>
in the Location Bar. With
>
javascript: document.open() ; function f(x) { document.write( x + "<br>");
f(x + 1); } f(0); document.close( );
>
it is 999, not 1000.
Same explanation, same suggestion to you. You simply cannot start
executing a function without executing it at least once. This is why
for top level tests you have to add 1 to the result. The only other
option is to watch the stack size itself on the system level.
>
A recursion does not occur until the method calls itself. So it is 999
recursions, if that.
Recursion starts then you call a function, because it still requires a
return point to store. Even such everyday trivia like
myFunction(args );
is in fact a "micro recursion" one level deep with the return point
set to the code immediately following the function call.
>
I am not changing anything; your test is flawed (and includes a lot of
unnecessary code again). It should count forwards, not backwards.
So change it as you like. I used this script to find the maximum, not
minimum, so it was more convenient to me to change max values and
count to zero. Just rebuild the loop to go to the opposite side.
On May 17, 11:44 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
VK wrote:
>
>
You are not making sense.
>
>
>
>
>
>
There is no loop.
>Firefox 2.0.0.14 - 1000 iterations limit
And if you meant recursions instead of iterations,
And if you meant recursions instead of iterations,
Well, we are changing the amount of iterations to check the allowed
limit of recursions :-)
limit of recursions :-)
You are not making sense.
>
but you are right with the correction.
(function f(x)
{
console.log(x);
f(x + 1);
})(0);
{
console.log(x);
f(x + 1);
})(0);
stops with a stack overflow after logging 994.
Tested in Firebug 1.1.0b12, Firefox 2.0.0.14.
You are missing 6 recursions because
1) 2 are used even before going to the loop:
1) 2 are used even before going to the loop:
There is no loop.
for anonymous function wrapper ()() and for f() inside it.
No, it is the same number if I use
>
function f(x)
{
console.log(x);
f(x + 1);
}
>
f(0);
f(x) for the initial enter. The only difference from the first option
is that here the first stack position is taken by a named function and
in the first example - by an anonymous function. Sorry, but you really
should refresh the stack mechanics theory during this week-end.
2) The rest is used for anonymous wrappers for your code in Firebug.
[...]
[...]
It is the same number when using
>
javascript: function f(x) { console.log(x); f(x + 1); } f(0);
>
in the Location Bar. With
>
javascript: document.open() ; function f(x) { document.write( x + "<br>");
f(x + 1); } f(0); document.close( );
>
it is 999, not 1000.
executing a function without executing it at least once. This is why
for top level tests you have to add 1 to the result. The only other
option is to watch the stack size itself on the system level.
Drop Firebug and use this instead:
Again, 999 because one stack position is already used for the entry
point.
Again, 999 because one stack position is already used for the entry
point.
A recursion does not occur until the method calls itself. So it is 999
recursions, if that.
return point to store. Even such everyday trivia like
myFunction(args );
is in fact a "micro recursion" one level deep with the return point
set to the code immediately following the function call.
Change to 1000 to break the script.
I am not changing anything; your test is flawed (and includes a lot of
unnecessary code again). It should count forwards, not backwards.
minimum, so it was more convenient to me to change max values and
count to zero. Just rebuild the loop to go to the opposite side.
Comment