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. Advertisements

  2. 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. Advertisements

  3. That is the one that you labelled [3].


    That is [3].
    No, but I think that you confused the alert() you call [3] and
    It does not get that far. The script gets aborted AFAICS.
    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. [snip]
    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. 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. 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. 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. Sorry. Internet Explorer 9.0.8112.16421 under Windows 7.
    I am not getting that. And there would be variability on the
    index number.
    Sincerely,

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

    [snip]
    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.
    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.

    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,
     
    Dr J R Stockton, Nov 18, 2011
    #10
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.