Large, Filled Arrays: What is Happening?


G

Gene Wirchenko

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
 
Ad

Advertisements

D

Denis McMahon

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
 
G

Gene Wirchenko

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 said:
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
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
 
G

Gene Wirchenko

[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
 
R

Richard Cornford

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

Denis McMahon

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
 
Ad

Advertisements

G

Gene Wirchenko

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
 
G

Gene Wirchenko

Gene said:
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
 
G

Gene Wirchenko

On Thu, 17 Nov 2011 06:34:48 -0800 (PST), Richard Cornford

[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
 
Ad

Advertisements

D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
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,
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top