469,612 Members | 2,658 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Accepted manner of clearing an array

I can think of several easy ways to clear out an array. The most
obvious:

arr=[];
arr.length=0;
arr=null;

What do most JavaScript programmers expect to see? Since JS is
transmitted, do I typically just standardize on the most compact form?
Jan 18 '08 #1
4 1680
On Jan 18, 10:36 am, timothytoe <timothy...@gmail.comwrote:
I can think of several easy ways to clear out an array. The most
obvious:

arr=[];
This doesn't "clear out" the array. This changes the array to which
the "arr" variable points. If two variables point to the array to be
cleared out then this won't work

var a = [0,1,2];
var b = a;

a = [];
console.log(a) // []
console.log(b) // [0,1,2]
arr.length=0;
This seems like a good option if the objective is to really clear out
the array.
arr=null;
same problem as the first option
What do most JavaScript programmers expect to see?
It depends what your goal is, exactly.
Since JS is
transmitted, do I typically just standardize on the most compact form?
Size is only one constraint. Correct and fast execution are important
too.

Peter
Jan 18 '08 #2
Peter Michaux wrote:
On Jan 18, 10:36 am, timothytoe <timothy...@gmail.comwrote:
>I can think of several easy ways to clear out an array. The most
obvious:

arr=[];

This doesn't "clear out" the array. This changes the array to which
the "arr" variable points. If two variables point to the array to be
cleared out then this won't work

var a = [0,1,2];
var b = a;

a = [];
console.log(a) // []
console.log(b) // [0,1,2]
>arr.length=0;

This seems like a good option if the objective is to really clear out
the array.
>arr=null;

same problem as the first option
>What do most JavaScript programmers expect to see?

It depends what your goal is, exactly.
>Since JS is
transmitted, do I typically just standardize on the most compact form?

Size is only one constraint. Correct and fast execution are important
too.
And most transfers are gzipped anyway. It should matter preciously little.
Very large identifiers may make your code use more memory on the client,
though, since they have to be stored 'as are'. As will repeating much code.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 22 '08 #3
António Marques said the following on 1/21/2008 6:42 AM:
Peter Michaux wrote:
<snip>
>Size is only one constraint. Correct and fast execution are important
too.

And most transfers are gzipped anyway. It should matter preciously little.
Very large identifiers may make your code use more memory on the client,
though, since they have to be stored 'as are'. As will repeating much code.
What makes you think that identifiers - large or small - make any
difference in memory usage in a browser? The only thing it impacts is
file size.

var
MyVariableWithAReallyLongNameJustToHaveAReallyLong NameSoICanWonderHowMuchMemoryItWillTakeToStoreThis VariableNameAsComparedToAShortVariableNameSuchAsTh eLetterA

The issue, other than file size, that comes in with long identifiers is
the chances increase with every letter of making a typo the next time
you type out the identifier.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 22 '08 #4
On Jan 21, 9:42 pm, António Marques <m...@sapo.ptwrote:
Peter Michaux wrote:
On Jan 18, 10:36 am, timothytoe <timothy...@gmail.comwrote:
[...]
Since JS is
transmitted, do I typically just standardize on the most compact form?
Size is only one constraint. Correct and fast execution are important
too.

And most transfers are gzipped anyway. It should matter preciously little.
It does matter as file sizes are relative - double the size of your
script file and likely it will be double when zipped also.

However, a few extra characters here and there for the sake of
legibility aren't likely to matter.
--
Rob
Jan 23 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Jamie Burns | last post: by
28 posts views Thread by Terry Andersen | last post: by
10 posts views Thread by Liz - Newbie | last post: by
65 posts views Thread by Steven Watanabe | last post: by
13 posts views Thread by Adam Honek | last post: by
3 posts views Thread by Martin | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.