make array empty

S

Sunny

Hi, Can someone tell me, How to redefine array or make array empty or
null.

Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.length-1; i++) {
temp = outstring.split(',');
}

Once the for loop will finish doing it, I want temp array to be null
or blank.
How Can I do that?
I tried doing temp.length =0 but that didn't work.
 
L

Laser Lips

Hi, Can someone tell me, How to redefine array or make array empty or
null.

Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.length-1; i++) {
temp = outstring.split(',');

}

Once the for loop will finish doing it, I want temp array to be null
or blank.
How Can I do that?
I tried doing temp.length =0 but that didn't work.


temp=[];
 
R

RobG

  var temp = [];   // unless see below [*] [...]

[*] That is, unless you're going push() a lot of elements on the array,
and you know the final number in advance; then it's more efficient to
use "new Array(num_elements)". I'm not exactly sure why that is, because
the implementations don't reserve memory in advance; maybe it's that the
.length property doesn't change after each push().

Have you compared that to using a while loop? e.g.

var t = [];
var i = array.length;

while (i--) {
t.push(array);
}

That should only set length once too, but I imagine the internal
[[put]] method must still check whether length needs to be
incremented, even if it doesn't have to actually do it.
 
S

sasuke

Hi, Can someone tell me, How to redefine array or make array empty or
null.
Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.length-1; i++) {
temp = outstring.split(',');
}


I assume you're doing something else in the loop too, or this would be
pretty pointless.

First of all, you don't need

  var temp = new Array();

because you'll immediately assign something else to temp in the loop.

  var temp;

will do. By the way, if you do want to create an empty array, use an
array literal:

  var temp = [];   // unless see below [*]
Once the for loop will finish doing it, I want temp array to be null
or blank.

Why? Just let it go out of scope, and it will be garbage collected
(unless there are closures present).
How Can I do that?
I tried doing temp.length =0 but that didn't work.

Are you really sure about that?
How didn't it work?

Alternatively, you could also use the delete operator, or you could
assign something else to temp, if you want, like null or [].

  - Conrad

[*] That is, unless you're going push() a lot of elements on the array,
and you know the final number in advance; then it's more efficient to
use "new Array(num_elements)". I'm not exactly sure why that is, because
the implementations don't reserve memory in advance; maybe it's that the
.length property doesn't change after each push().


IMO, memory is reserved for the data structure which is responsible
for maintaining the state of the Array object; it can be an Object[]
of Java or an array of void pointers in C. The advantage AFAIK here is
that setting the 'length' property avoids a lot of copying of
references to and fro when inserting an element since each time the
backing data store has to be expanded to take in the new element i.e.
ensureCapacity does nothing as long as the number of elements
sequentially inserted is less than the specified length.

Even I think that setting the length property to 0 should have done
the job though setting it to null or just letting it go out of scope
of more convenient and processing friendly [since setting the length
or deleting actually loops over the array elements].
 
J

John G Harris

On Wed, 8 Oct 2008 at 22:53:26, in comp.lang.javascript, sasuke wrote:

IMO, memory is reserved for the data structure which is responsible
for maintaining the state of the Array object; it can be an Object[]
of Java or an array of void pointers in C. The advantage AFAIK here is
that setting the 'length' property avoids a lot of copying of
references to and fro when inserting an element since each time the
backing data store has to be expanded to take in the new element i.e.
ensureCapacity does nothing as long as the number of elements
sequentially inserted is less than the specified length.

It can't be as simple as that. Try putting this into the address bar and
executing it :

javascript: var a1 = new Array(); a1[2000000000] = "Wow"; alert(a1.length);

It doesn't blow up. Neither does this :

javascript: var a1 = new Array(2000000000); alert(a1.length );

Even I think that setting the length property to 0 should have done
the job though setting it to null or just letting it go out of scope
of more convenient and processing friendly [since setting the length
or deleting actually loops over the array elements].

According to ECMA 262, setting the length to zero should indeed do the job :

"whenever the length property is changed, every property whose
name is an array index whose value is not smaller than the new length is
automatically deleted."

As you say, replacing the variable's value by a new array or null will also do
the job. Whether it is quicker depends on how the garbage collector works.

John
 
S

sasuke

On Wed, 8 Oct 2008 at 22:53:26, in comp.lang.javascript, sasuke wrote:

  said:
IMO, memory is reserved for the data structure which is responsible
for maintaining the state of the Array object; it can be an Object[]
of Java or an array of void pointers in C. The advantage AFAIK here is
that setting the 'length' property avoids a lot of copying of
references to and fro when inserting an element since each time the
backing data store has to be expanded to take in the new element i.e.
ensureCapacity does nothing as long as the number of elements
sequentially inserted is less than the specified length.

It can't be as simple as that. Try putting this into the address bar and
executing it :

  javascript: var a1 = new Array(); a1[2000000000] = "Wow"; alert(a1.length);

It doesn't blow up. Neither does this :

  javascript: var a1 = new Array(2000000000);  alert(a1.length );

My apologies, I should have been more explicit. Though there is no
mention of such in the specification, almost all implementations have
a `sparse' or `dense' flag which is used to mark the Array object.
This flag demands a suitable processing on part of the implementation
when a high value of index is used; which explains your first e.g.

As for the second e.g., try something like javascript: var a1 = new
Array(2000000000); for(var i = 0, maxI = a1.length; i < maxI; ++i)
{ a1 = Number(i); }

The script stops responding, doesn't it? This gives us an indication
that the implementation is smart enough to allocate memory to the
Array object when it actually is required [maybe the length passed to
the Array constructor isn't used till a reference to a particular
location isn't made].
Even I think that setting the length property to 0 should have done
the job though setting it to null or just letting it go out of scope
of more convenient and processing friendly [since setting the length
or deleting actually loops over the array elements].

According to ECMA 262, setting the length to zero should indeed do the job :

"whenever the length property is changed, every property whose
name is an array index whose value is not smaller than the new length is
automatically deleted."

As you say, replacing the variable's value by a new array or null will also do
the job. Whether it is quicker depends on how the garbage collector works..

Yes, what needs to be done greatly depends on the implementation
details which makes the question "which is the fastest way to reclaim
memory taken by the Array xxx" way too generic or difficult to answer.

/sasuke
 
D

dhtml

ECMAScript does not have real arrays. Arrays are objects.
It can't be as simple as that. Try putting this into the address bar and
executing it :

javascript: var a1 = new Array(); a1[2000000000] = "Wow"; alert(a1.length);

That creates a new Array with two properties: 2000000000 and length.

2000000000 = "Wow"
length = 2000000001
It doesn't blow up. Neither does this :

javascript: var a1 = new Array(2000000000); alert(a1.length );

That creates a new Array with one property: length.

Firefox <= 3.0.3 exhibits a bug where an array is 'prefilled' with
properties having undefined values.

javascript:alert('2' in [,,,,,,,,,,])

true in firefox 3.0.3

(should be false).


Garrett
 
T

Thomas 'PointedEars' Lahn

sasuke said:
[...] Though there is no mention of such in the specification, almost all
implementations have a `sparse' or `dense' flag which is used to mark the
Array object. This flag demands a suitable processing on part of the
implementation when a high value of index is used; which explains your
first e.g. [...]

How do you got *that* idea?
As for the second e.g., try something like javascript: var a1 = new
Array(2000000000); for(var i = 0, maxI = a1.length; i < maxI; ++i) {
a1 = Number(i); }

The script stops responding, doesn't it?


Responding to what?
This gives us an indication that the implementation is smart enough to
allocate memory to the Array object when it actually is required [maybe
the length passed to the Array constructor isn't used till a reference to
a particular location isn't made].

Your logic is flawed. The *user agent* stops responding (and may show the
user a dialog that allows them to stop execution of the script) because the
script is running quite a long time and all known ECMAScript implementations
are single-threaded. It is completely irrelevant that an Array object is
involved here. Simple proof:

while (true);


PointedEars
 
S

sasuke

sasuke said:
[...] Though there is no mention of such in the specification, almost all
implementations have a `sparse' or `dense' flag which is used to mark the
Array object. This flag demands a suitable processing on part of the
implementation when a high value of index is used; which explains your
first e.g. [...]

How do you got *that* idea?

When sifting through the source code of Rhino; the Java implementation
of ECMAScript. The NativeArray class maintains a flag called
'denseOnly'.
As for the second e.g., try something like javascript: var a1 = new
Array(2000000000);  for(var i = 0, maxI = a1.length; i < maxI; ++i) {
a1 = Number(i); }

The script stops responding, doesn't it?

Responding to what?
This gives us an indication that the implementation is smart enough to
allocate memory to the Array object when it actually is required [maybe
the length passed to the Array constructor isn't used till a reference to
 a particular location isn't made].

Your logic is flawed.  The *user agent* stops responding (and may show the
user a dialog that allows them to stop execution of the script) because the
script is running quite a long time and all known ECMAScript implementations
are single-threaded.  It is completely irrelevant that an Array object is
involved here.  Simple proof:

  while (true);


I concur; I used a wrong example in trying to put forth my
observation. Another small experiment I tried was tracking the memory
usage when running the examples:

Case 1:
javascript: var a = new Array(200000000); for(var i = 0; i < 5000; +
+i) { a[i * 10] = i; }

Case 2:
javascript: var a = new Array(200000000); for(var i = 0; i < 5000; +
+i) { var j = i + 1; }

Case 3:
javascript: var a = new Array(200000000); a[1000] = 1000;

The observations were that when running e.g. 1, there was a sharp
memory spike of the order of several KB's which wasn't seen when
running e.g. 2 and 3.

I agree that these small tests don't prove anything; just an
observation, that's it.
 
T

Thomas 'PointedEars' Lahn

sasuke said:
Thomas said:
sasuke said:
[...] Though there is no mention of such in the specification, almost all
implementations have a `sparse' or `dense' flag which is used to mark the
Array object. This flag demands a suitable processing on part of the
implementation when a high value of index is used; which explains your
first e.g. [...]
How do you got *that* idea?

When sifting through the source code of Rhino; the Java implementation
of ECMAScript. The NativeArray class maintains a flag called
'denseOnly'.

While that is interesting, this is but one implementation, the wrong one for
the discussed example (we are talking SpiderMonkey and friends in this
thread), and it does not explain the example (whereas it is unclear what
exactly needed explaining there).


PointedEars
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top