By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

Accepted manner of clearing an array

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.