Large, Filled Arrays: What is Happening?

Discussion in 'Javascript' started by Gene Wirchenko, Nov 17, 2011.

  1. Dear JavaScripters:

    Were I to do
    alert("junk="+junk);
    and junk had not been declared, it would be an error. If junk had
    been declared, the value would be displayed. This might be undefined.

    With an array, the element need not have been assigned to to get
    this behaviour. Declaring the array means that
    alert("element="+theArray);
    for some i will not throw an error on the element being undefined.

    If I create a sparse array element, it is quick, as in:
    theArray=new Array();
    theArray[999999999]="way out there";

    With the above figured out -- if I am at all wrong in the above,
    please tell me -- I thought that I would check performance on large,
    filled-in arrays.

    The following code gives interesting results on my system. I
    have played around with initial values for Expand and how much it is
    incremented at the end of the loop.

    When I try to get it to do the looping for expansion to
    130,000,000, I get the confirmation box which I OK, I get the alert
    box that it is starting, and THEN after a delay, I get the "Done"
    text. I do not get the alert box that the looping is complete nor the
    one that outputs fLooping. There is no error from my browser either.

    Why is what is happening happening?

    ***** Start of Code *****
    <html>

    <head>
    <title>try5.html</title>

    <script type="text/javascript">
    </script>

    </head>

    <body>

    <script type="text/javascript">

    theArray=new Array();
    var OldMax=0;
    var Expand=129000000;
    var fLooping=true;
    while (fLooping)
    {
    fLooping=confirm("Confirm do Expand="+Expand);
    if (fLooping)
    {
    alert("doing Expand="+Expand);
    for (var i=OldMax; i<Expand; i++)
    theArray=i;
    alert("done Expand="+Expand);
    OldMax=Expand;
    Expand+=100000;
    }
    }
    alert("fLooping="+fLooping);

    </script>

    Done

    </body>

    </html>
    ***** End of Code *****

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 17, 2011
    #1
    1. Advertising

  2. On Wed, 16 Nov 2011 17:17:57 -0800, Gene Wirchenko wrote:

    > When I try to get it to do the looping for expansion to
    > 130,000,000, I get the confirmation box [1] which I OK, I get the alert

    box
    > that it is starting [2], and THEN after a delay, I get the "Done" text

    [3]. I
    > do not get the alert box that the looping is complete [???] nor the one

    that
    > outputs fLooping [4]. There is no error from my browser either.


    Looking just at the script element, and with a few markers added by
    myself:

    <script type="text/javascript">
    theArray=new Array();
    var OldMax=0;
    var Expand=129000000;
    var fLooping=true;
    while (fLooping)
    { // while (fLooping) ->
    fLooping=confirm("Confirm do Expand="+Expand); // [1]
    if (fLooping)
    { // if (fLooping) ->
    alert("doing Expand="+Expand); // [2]
    for (var i=OldMax; i<Expand; i++) theArray=i;
    alert("done Expand="+Expand); // [3]
    OldMax=Expand;
    Expand+=100000;
    } // <- if (fLooping)
    } // <- while (fLooping)
    alert("fLooping="+fLooping); // [4]
    </script>

    I'm having trouble working out which is the first alert that you don't
    get, where you say "I do not get the alert box that the looping is
    complete".

    You describe 5 alerts in your explanation of what happens, but you only
    have 4 alerts. I would assume that after a successful expand for
    130,000,000, the next alert / dialog would be the confirm dialog for
    131,000,000, but that doesn't match your description of the alert box
    that you're expecting but don't receive.

    If you are expecting the alert:

    alert("fLooping="+fLooping);

    to appear at the end of each loop, you need to include it *inside* the
    while loop, at the moment it will only trigger after the while loop has
    exited.

    Rgds

    Denis McMahon
     
    Denis McMahon, Nov 17, 2011
    #2
    1. Advertising

  3. On 17 Nov 2011 02:15:37 GMT, Denis McMahon <>
    wrote:

    >On Wed, 16 Nov 2011 17:17:57 -0800, Gene Wirchenko wrote:
    >
    >> When I try to get it to do the looping for expansion to
    >> 130,000,000, I get the confirmation box [1] which I OK, I get the alert

    >box
    >> that it is starting [2], and THEN after a delay, I get the "Done" text

    >[3]. I


    By "Done" text, I mean the "Done" after the </script>.

    >> do not get the alert box that the looping is complete [???] nor the one

    >that


    That is the one that you labelled [3].

    >> outputs fLooping [4]. There is no error from my browser either.

    >
    >Looking just at the script element, and with a few markers added by
    >myself:
    >
    ><script type="text/javascript">
    >theArray=new Array();
    >var OldMax=0;
    >var Expand=129000000;
    >var fLooping=true;
    >while (fLooping)
    > { // while (fLooping) ->
    > fLooping=confirm("Confirm do Expand="+Expand); // [1]
    > if (fLooping)
    > { // if (fLooping) ->
    > alert("doing Expand="+Expand); // [2]
    > for (var i=OldMax; i<Expand; i++) theArray=i;
    > alert("done Expand="+Expand); // [3]
    > OldMax=Expand;
    > Expand+=100000;
    > } // <- if (fLooping)
    > } // <- while (fLooping)
    >alert("fLooping="+fLooping); // [4]
    ></script>
    >
    >I'm having trouble working out which is the first alert that you don't
    >get, where you say "I do not get the alert box that the looping is
    >complete".


    That is [3].

    >You describe 5 alerts in your explanation of what happens, but you only
    >have 4 alerts. I would assume that after a successful expand for


    No, but I think that you confused the alert() you call [3] and
    the "Done" after the </script>. In retrospect, that was not the
    clearest writing on my part.

    >130,000,000, the next alert / dialog would be the confirm dialog for
    >131,000,000, but that doesn't match your description of the alert box
    >that you're expecting but don't receive.


    It does not get that far. The script gets aborted AFAICS.

    >If you are expecting the alert:
    >
    >alert("fLooping="+fLooping);
    >
    >to appear at the end of each loop, you need to include it *inside* the
    >while loop, at the moment it will only trigger after the while loop has
    >exited.


    I am not. I put that there when I saw that the "Done" after the
    </script> was getting output. I did not think that fLooping would
    have been changed -- since I only do it in the confirm() line -- but I
    wanted to be sure. It does not get executed at all AFAICS.

    Why is the script getting aborted and why is there no error
    thrown?

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 17, 2011
    #3
  4. On Wed, 16 Nov 2011 21:13:19 -0800, Gene Wirchenko <>
    wrote:

    [snip]

    > Why is the script getting aborted and why is there no error
    >thrown?


    I have cut it down further. The script consistently dies when
    assigning to theArray[129954080] if all elements up to that point have
    been assigned to. In hexadecimal, the index is 0x7BEF120 which does
    not appear significant in itself.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 17, 2011
    #4
  5. On Nov 17, 9:31 am, Richard Cornford wrote:
    > Gene Wirchenko wrote:
    >> On Wed, 16 Nov 2011 21:13:19 -0800, Gene Wirchenko wrote:

    >
    >>> Why is the script getting aborted and why is there no error
    >>>thrown?

    >
    >> I have cut it down further. The script consistently dies
    >> when assigning to theArray[129954080] if all elements up to
    >> that point have been assigned to. In hexadecimal, the index
    >> is 0x7BEF120 which does not appear significant in itself.

    >
    > You haven't said which browsers you have tried this with. Browsers
    > tend to have a 'safety' feature where they will not allow scripts
    > to run indefinitely. IE, for example, has a limit on the number of
    > consecutive statements it will execute before it puts up a dialog
    > asking the user if they want to abort the script. Other browsers
    > have been observed to put up a similar dialog if a single script
    > runs for longer than a pre-determined period. It is also possible
    > that some browsers/configurations of browsers could abort long
    > running scripts without putting up any warnings. Getting consistent
    > numbers prior to a script's being aborted on a single
    > browsers/hardware combination may be a coincidence against a fixed
    > time period or it may be just running into the browser's limit of
    > consecutive statement execution.


    In IE 9 the problem is an "out of memory" error that can be seen if
    you open the 'developer tools' (F12) and look at the Script tab.
    Apparently IE no longer throws javascript exceptions when it
    encounters out of memory errors (it did in version <= 8).

    Richard.
     
    Richard Cornford, Nov 17, 2011
    #5
  6. On Wed, 16 Nov 2011 21:27:43 -0800, Gene Wirchenko wrote:

    > I have cut it down further. The script consistently dies when
    > assigning to theArray[129954080] if all elements up to that point have
    > been assigned to. In hexadecimal, the index is 0x7BEF120 which does not
    > appear significant in itself.


    Perhaps it represents a specific amount of ram?

    Although I can break javascript by assigning a large enough array, this
    doesn't seem to be a catchable error. eg:

    var a = new Array(),i=0;
    try {
    while(1) a[i++] = 0;
    }
    catch(e) {
    document.write(e.description);
    }

    seems to use cpu for a while and then just stop, without outputting
    anything.

    Rgds

    Denis McMahon


    Rgds

    Denis McMahon
     
    Denis McMahon, Nov 17, 2011
    #6
  7. On 17 Nov 2011 14:48:08 GMT, Denis McMahon <>
    wrote:

    >On Wed, 16 Nov 2011 21:27:43 -0800, Gene Wirchenko wrote:
    >
    >> I have cut it down further. The script consistently dies when
    >> assigning to theArray[129954080] if all elements up to that point have
    >> been assigned to. In hexadecimal, the index is 0x7BEF120 which does not
    >> appear significant in itself.

    >
    >Perhaps it represents a specific amount of ram?


    Apparently.

    >Although I can break javascript by assigning a large enough array, this
    >doesn't seem to be a catchable error. eg:
    >
    >var a = new Array(),i=0;
    >try {
    >while(1) a[i++] = 0;
    >}
    >catch(e) {
    >document.write(e.description);
    >}
    >
    >seems to use cpu for a while and then just stop, without outputting
    >anything.


    At some point, the script is simply aborted, and page rendering
    continues past that point.

    Well, it was a test of the limits, and an error message would
    have been nice.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 17, 2011
    #7
  8. On Thu, 17 Nov 2011 09:31:32 -0000, "Richard Cornford"
    <> wrote:

    >Gene Wirchenko wrote:
    >> On Wed, 16 Nov 2011 21:13:19 -0800, Gene Wirchenko wrote:
    >>
    >> [snip]
    >>
    >>> Why is the script getting aborted and why is there no error
    >>>thrown?

    >>
    >> I have cut it down further. The script consistently dies
    >> when assigning to theArray[129954080] if all elements up to
    >> that point have been assigned to. In hexadecimal, the index
    >> is 0x7BEF120 which does not appear significant in itself.

    >
    >You haven't said which browsers you have tried this with. Browsers tend


    Sorry. Internet Explorer 9.0.8112.16421 under Windows 7.

    >to have a 'safety' feature where they will not allow scripts to run
    >indefinitely. IE, for example, has a limit on the number of consecutive
    >statements it will execute before it puts up a dialog asking the user if
    >they want to abort the script. Other browsers have been observed to put


    I am not getting that. And there would be variability on the
    index number.

    >up a similar dialog if a single script runs for longer than a
    >pre-determined period. It is also possible that some
    >browsers/configurations of browsers could abort long running scripts
    >without putting up any warnings. Getting consistent numbers prior to a
    >script's being aborted on a single browsers/hardware combination may be
    >a coincidence against a fixed time period or it may be just running into
    >the browser's limit of consecutive statement execution.


    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 17, 2011
    #8
  9. On Thu, 17 Nov 2011 06:34:48 -0800 (PST), Richard Cornford
    <> wrote:

    [snip]

    >In IE 9 the problem is an "out of memory" error that can be seen if
    >you open the 'developer tools' (F12) and look at the Script tab.


    I found that, and I had to rerun the script to get the error
    while the tool was open. It then stated out of memory in line 1. The
    error is not in line 1.

    >Apparently IE no longer throws javascript exceptions when it
    >encounters out of memory errors (it did in version <= 8).


    More experimenting: I tried a try...catch block around the loop.
    That caught an error: 0x800A0007, "Out of memory". Subsequent
    iterations apparently ran fine with no abort and no error. How, if
    out of memory, I do not know.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 17, 2011
    #9
  10. In comp.lang.javascript message <26o8c7pumvbu8ki4tc2qn1eo24q1dvmefb@4ax.
    com>, Wed, 16 Nov 2011 17:17:57, Gene Wirchenko <> posted:

    >
    > When I try to get it to do the looping for expansion to
    >130,000,000, I get the confirmation box which I OK, I get the alert
    >box that it is starting, and THEN after a delay, I get the "Done"
    >text. I do not get the alert box that the looping is complete nor the
    >one that outputs fLooping. There is no error from my browser either.
    >
    > Why is what is happening happening?



    You need to say which browser you are using, and what version of it.
    You need to say what debugging / error-reporting features are on and
    visible.

    You need either to say which OS and version you are using, or to explain
    that it does not matter.

    In this case you should say how much memory you have, or to explain that
    it does not matter.

    And, if you have any date/time problems, you need to say what Time Zone
    the computer is set for, and whether that location uses, and is in,
    Summer Time. Don't be in Israel, if avoidable.

    I don't think it is necessarily fair to say that the best browser is
    ABBIE,

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

    ABBIE is an acronym, probably new.
     
    Dr J R Stockton, Nov 18, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio D'Ottavio
    Replies:
    1
    Views:
    2,747
    Antonio D'Ottavio
    Sep 19, 2005
  2. John Saunders
    Replies:
    0
    Views:
    744
    John Saunders
    Aug 29, 2003
  3. John Timney \(Microsoft MVP\)

    Re: Listbox filled with Javascript does NOT maintain viewstate

    John Timney \(Microsoft MVP\), Aug 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    592
    John Timney \(Microsoft MVP\)
    Aug 29, 2003
  4. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=

    RE: bind datagrid to dataset filled at runtime

    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=, Apr 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,621
    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    Apr 8, 2004
  5. Philipp
    Replies:
    21
    Views:
    1,185
    Philipp
    Jan 20, 2009
Loading...

Share This Page