Javascript recursion limit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VK

    #16
    Re: Javascript recursion limit

    On May 17, 11:44 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
    wrote:
    VK wrote:
    >Firefox 2.0.0.14 - 1000 iterations limit
    And if you meant recursions instead of iterations,
    >
    Well, we are changing the amount of iterations to check the allowed
    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);
    >
    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:
    >
    There is no loop.
    Look closer.
    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);
    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.
    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.
    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.

    Drop Firebug and use this instead:
    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.
    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.
    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.
    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.

    Comment

    • Thomas 'PointedEars' Lahn

      #17
      Re: Javascript recursion limit

      VK wrote:
      On May 17, 11:44 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
      wrote:
      >VK wrote:
      >>>>Firefox 2.0.0.14 - 1000 iterations limit
      >>>And if you meant recursions instead of iterations,
      >>Well, we are changing the amount of iterations to check the allowed
      >>limit of recursions :-)
      >You are not making sense.
      Care to explain yourself?
      >>but you are right with the correction.
      >>>(function f(x) { 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:
      >There is no loop.
      >
      Look closer.
      There still is no loop. A loop would mean iteration, this is recursion.
      This is the second time in a row you confuse the two, I suggest you look
      them up.
      >>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);
      >
      because there are still two stack position used: for f(0) call and for
      f(x) for the initial enter.
      You have argued that the anonymous function wrapper would introduce one
      recursion; I have disproved that.
      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.
      Pot, cattle, black.
      >>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.
      >
      Same explanation, same suggestion to you.
      Same old idiot.
      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.
      No, I don't. There are 999 recursions possible, if that, not 1000.
      option is to watch the stack size itself on the system level.
      The stack size never yields the number of recursions.
      >>Drop Firebug and use this instead: 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.
      >
      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.
      Whatever the definitions in that parallel universe you must live in, in this
      universe in the strict sense a recursion is defined as a function calling
      itself. So there is no recursion until that happens.




      PointedEars
      --
      Use any version of Microsoft Frontpage to create your site.
      (This won't prevent people from viewing your source, but no one
      will want to steal it.)
      -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

      Comment

      • VK

        #18
        Re: Javascript recursion limit

        >You are missing 6 recursions because 1) 2 are used even before going
        >to the loop:
        There is no loop.
        >
        Look closer.
        >
        There still is no loop.
        Yet even more closer than :-)
        A loop would mean iteration, this is recursion.
        Ah, OK, I see your problem now. The function f calls itself over and
        over again until all allowed resources are used - it is an endless
        loop. The "loop" term is not exclusively attached neither to
        iterations nor to recursions.
        You have argued that the anonymous function wrapper would introduce one
        recursion; I have disproved that.
        where? not in this thread at least.
        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.
        >
        Pot, cattle, black.
        Is it a new vernacular form of "yes, of course"? Simply "OK" would be
        enough though...
        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.
        >
        No, I don't. There are 999 recursions possible, if that, not 1000.
        Just use the second test I have just posted. And if you want to add
        something useful to the discussion, I would suggest to play with
        function f() {}
        against of
        var f = function() {}
        in stack measurement.
        I bet it will be a lot of interesting here for people learning the
        language by specs instead of by actual implementations . I may make my
        test some later if I have time.

        Comment

        • Thomas 'PointedEars' Lahn

          #19
          Re: Javascript recursion limit

          VK wrote:
          >>>>You are missing 6 recursions because 1) 2 are used even before going
          >>>>to the loop:
          >>>There is no loop.
          >>Look closer.
          >There still is no loop.
          >
          Yet even more closer than :-)
          This is gibberish again.
          >A loop would mean iteration, this is recursion.
          >
          Ah, OK, I see your problem now. The function f calls itself over and
          over again until all allowed resources are used - it is an endless
          loop. The "loop" term is not exclusively attached neither to
          iterations nor to recursions.
          You are mistaken. When a function calls itself, i.e. recursion occurs, that
          is _not_ considered a loop. A loop is something that closes back on itself.
          To make it easier for you to understand the difference, a loop (i.e.
          iteration) looks as follows:

          * n
          ,---->----.
          | |
          -->o |---
          ^ |
          `----<----'

          The execution context remains the same with each loop.

          Recursion, on the other hand, looks like this:

          -->o-----------------
          ^
          |
          v
          o
          ^
          |
          v
          o

          Comment

          • Lasse Reichstein Nielsen

            #20
            Re: Javascript recursion limit

            Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
            You are mistaken. When a function calls itself, i.e. recursion occurs, that
            is _not_ considered a loop.
            That depends.
            A loop is something that closes back on itself.
            I.e., a loop is something that repeats - that comes back to the
            same state, in some sense (not the exact same state, since that would
            cause an infinite loop, but the same state wrt. control flow).


            An recursive function can do that, if the language allows it.
            In languages with proper tail recursion, a recursive function is the
            way to create loops. E.g.,

            fun sum nil acc = 0
            | sum (x::xs) acc = sum xs (acc+x)

            In ECMAScript terms: While the called object remains the same, the execution
            context does not.
            True. ECMAScript does not require an implementation to have tail recursion,
            and even then, not all recursion is tail recursion.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Thomas 'PointedEars' Lahn

              #21
              Re: Javascript recursion limit

              VK wrote:
              On May 18, 2:16 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
              wrote:
              >A recursive algorithm does not *close back* *on* itself, it *calls forward*
              >*to* itself. Hence the requirement of a stack for recursion, but not for
              >iteration.
              >
              OK, let's us set the grounds first: iterations, recursions, finite
              loops, infinite loops etc. these are all top level mental constructs
              used in the programming to distinguish certain types of top level
              coding. On the engine level itself there is the stack, its size and
              respectively certain amount of RET values it may store in LIFO schema.
              Respectively the engine itself doesn't care what RETs are these:
              function F0001 calling F0002,... calling F1000 - or the same function
              F calling itself 1000 times. It is all the same by the allocation
              demands.
              No, it is not. With recursion it is a different execution context than
              iteration, and therefore the allocation demands must be quite different.

              Besides, arguing with assembler when we know that we are dealing with a
              Virtual Machine, and using eval() for testing recursion, shows again what
              little your long-winded "explanatio ns", flawed "tests", and bloated
              "examples" are worth.


              PointedEars
              --
              var bugRiddenCrashP ronePieceOfJunk = (
              navigator.userA gent.indexOf('M SIE 5') != -1
              && navigator.userA gent.indexOf('M ac') != -1
              ) // Plone, register_functi on.js:16

              Comment

              • VK

                #22
                Re: Javascript recursion limit

                On May 18, 2:25 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                wrote:
                No, it is not. With recursion it is a different execution context than
                iteration, and therefore the allocation demands must be quite different.
                >
                Besides, arguing with assembler when we know that we are dealing with a
                Virtual Machine, and using eval() for testing recursion, shows again what
                little your long-winded "explanatio ns", flawed "tests", and bloated
                "examples" are worth.
                Do you want to learn and to find the real nature of things or just
                being nasty on everyone? For the latter just start a new thread then
                like "Why does the world suck" or similar so do not be OT. For the
                first remove eval() wrapper and call the function directly:
                //eval(fun[0](0));
                fun[0](0);
                That takes one extra RET position in the stack for the function test()
                from where the initial call is made, so you are getting 998 instead of
                999.


                Comment

                • Peter Michaux

                  #23
                  Re: Javascript recursion limit

                  On May 17, 4:27 am, Lasse Reichstein Nielsen <l...@hotpop.co mwrote:
                  Jeff Bigham <jeffrey.big... @gmail.comwrite s:
                  Is recursion not a viable option in Javascript?
                  >
                  As viable as in any other *language* - i.e., limited by the implementation
                  and/or platform that it runs under.
                  Javascript in web pages just has the extra problem that the author doesn't
                  get to pick the language implementation.
                  I would say there are languages where recursion is more viable than
                  JavaScript. Any language that guarantees proper tail call elimination
                  would make it possible to program with tail call recursion and no
                  worries about call stack depth. Scheme is one language that makes this
                  guarantee.

                  Unfortunately one of the long standing ECMAScript 4 features was going
                  to be proper tail calls but apparently that made type checking too
                  difficult. They decided that type checking was more important. [waits
                  for gasp to end] I know. I was very disappointed also. I don't give a
                  hoot about type checking but I do care about functional programming a
                  lot. Oh well. That about sums up my feelings on ES4.

                  Peter

                  Comment

                  • Jorge

                    #24
                    Re: Javascript recursion limit

                    Jeff Bigham wrote:
                    So, it appears that Javascript has a recursion limit of about 1000
                    levels on FF, maybe less/more on other browsers.
                    javascript:(fun ction f(p){document.w rite(p+'<br>'); f(p+1); })(0)

                    WebKit/Safari r33029 --139808
                    FF3.0pre --2999
                    IE8.0.6001 --2340

                    --Jorge

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #25
                      Re: Javascript recursion limit

                      Jorge wrote:
                      Jeff Bigham wrote:
                      >So, it appears that Javascript has a recursion limit of about 1000
                      >levels on FF, maybe less/more on other browsers.
                      >
                      javascript:(fun ction f(p){document.w rite(p+'<br>'); f(p+1); })(0)
                      >
                      WebKit/Safari r33029 --139808
                      FF3.0pre --2999
                      I presume you mean Fx 3 RC 1 because I get that number there.
                      IE8.0.6001 --2340
                      IE 7.0.5730.11 --2507


                      PointedEars
                      --
                      Anyone who slaps a 'this page is best viewed with Browser X' label on
                      a Web page appears to be yearning for the bad old days, before the Web,
                      when you had very little chance of reading a document written on another
                      computer, another word processor, or another network. -- Tim Berners-Lee

                      Comment

                      • Evertjan.

                        #26
                        Re: Javascript recursion limit

                        Thomas 'PointedEars' Lahn wrote on 23 mei 2008 in comp.lang.javas cript:
                        IE 7.0.5730.11 --2507
                        Mine (IE 7.0.5730.11 under XP) does 2553

                        =============== =====
                        function f(p){
                        if (!(p%500)||p>25 50)
                        document.write( p+'<br>');
                        f(p+1);
                        };
                        f(0);
                        =============== =====

                        Why the difference?
                        XP - Vista??

                        It gives a popup warning "Out of memory".

                        Limited "subroutine " return stack?

                        Other memory use does not make any difference:

                        =============== =====
                        function f(p){
                        if (!(p%500)||p>25 50)
                        document.write( p+'<br>');
                        var z = 'Hello world';
                        f(p+1);
                        };

                        f(0);
                        =============== =====

                        --
                        Evertjan.
                        The Netherlands.
                        (Please change the x'es to dots in my emailaddress)

                        Comment

                        • VK

                          #27
                          Re: Javascript recursion limit

                          On May 23, 11:29 am, "Evertjan." <exjxw.hannivo. ..@interxnl.net >
                          wrote:
                          Thomas 'PointedEars' Lahn wrote on 23 mei 2008 in comp.lang.javas cript:
                          >
                          IE 7.0.5730.11 --2507
                          >
                          Mine (IE 7.0.5730.11 under XP) does 2553
                          >
                          =============== =====
                          function f(p){
                          if (!(p%500)||p>25 50)
                          document.write( p+'<br>');
                          f(p+1);};
                          >
                          f(0);
                          document.write method is implemented as a pipe so useless for stack
                          studies. For the actual stack studies use the test I poster earlier
                          at

                          Comment

                          • Dr J R Stockton

                            #28
                            Re: Javascript recursion limit

                            In comp.lang.javas cript message <48360ACD.10001 04@PointedEars. de>, Fri,
                            23 May 2008 02:07:41, Thomas 'PointedEars' Lahn <PointedEars@we b.de>
                            posted:
                            >Jorge wrote:
                            >Jeff Bigham wrote:
                            >>So, it appears that Javascript has a recursion limit of about 1000
                            >>levels on FF, maybe less/more on other browsers.
                            >>
                            >javascript:(fu nction f(p){document.w rite(p+'<br>'); f(p+1); })(0)
                            >IE 7.0.5730.11 --2507
                            IE 7.0.5730.13 --2507 with that pasted into the address bar.

                            With (function f(p){document.w rite(p+'<br>'); f(p+1); })(0) in
                            <URL:http://www.merlyn.demo n.co.uk/js-quick.htm: Eval 2523, NewW 2523.

                            --
                            (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
                            news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
                            <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                            Comment

                            • Evertjan.

                              #29
                              Re: Javascript recursion limit

                              VK wrote on 24 mei 2008 in comp.lang.javas cript:
                              On May 23, 11:29 am, "Evertjan." <exjxw.hannivo. ..@interxnl.net >
                              wrote:
                              >Thomas 'PointedEars' Lahn wrote on 23 mei 2008 in comp.lang.javas cript:
                              >>
                              IE 7.0.5730.11 --2507
                              >>
                              >Mine (IE 7.0.5730.11 under XP) does 2553
                              >>
                              >============== ======
                              >function f(p){
                              > if (!(p%500)||p>25 50)
                              > document.write( p+'<br>');
                              > f(p+1);};
                              >>
                              >f(0);
                              >
                              document.write method is implemented as a pipe so useless for stack
                              studies.
                              I don't see why. Please explain.

                              --
                              Evertjan.
                              The Netherlands.
                              (Please change the x'es to dots in my emailaddress)

                              Comment

                              • VK

                                #30
                                Re: Javascript recursion limit

                                On May 24, 7:13 pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
                                VK wrote on 24 mei 2008 in comp.lang.javas cript:
                                >
                                >
                                >
                                On May 23, 11:29 am, "Evertjan." <exjxw.hannivo. ..@interxnl.net >
                                wrote:
                                Thomas 'PointedEars' Lahn wrote on 23 mei 2008 in comp.lang.javas cript:
                                >
                                IE 7.0.5730.11 --2507
                                >
                                Mine (IE 7.0.5730.11 under XP) does 2553
                                >
                                =============== =====
                                function f(p){
                                if (!(p%500)||p>25 50)
                                document.write( p+'<br>');
                                f(p+1);};
                                >
                                f(0);
                                >
                                document.write method is implemented as a pipe so useless for stack
                                studies.
                                >
                                I don't see why. Please explain.
                                I was wrong - it did change.
                                So coming back to the original issue, the maximum universal stack size
                                is defined by the smallest value among the browsers in considerations.
                                Also the factual stack size is at least 1 position smaller than the
                                physical stack size because the position[0] is always taken by the RET
                                address of the initial entry point into current context, so at least
                                RET 0 (exit from the current context).
                                This way if we do care about Safari then the universal max of RET
                                points is 497 (Safari 3.x 498-1)
                                Without Safari in consideration universal max of RET points is 999
                                (Firefox 2.x 1000-1)

                                Comment

                                Working...