Accepted manner of clearing an array

T

timothytoe

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?
 
P

Peter Michaux

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
 
A

António Marques

Peter said:
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.
 
R

RobG

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

Thomas 'PointedEars' Lahn

RobG said:
It does matter as file sizes are relative - double the size of your
script file and likely it will be double when zipped also.

gzip would not be used much if that were the case.

The probability that the resulting (compressed) file is smaller than two
times the size of the original (uncompressed) file is greater than the
opposite case, because redundancy tends to be greater the more data is
accumulated. The DEFLATE algorithm of gzip is a combination of LZ77 and
Huffman coding, the latter providing one of the strongest lossless
compressions possible.

As a side note, that is one reason I mentioned before why it would be a Good
Thing to write element and attribute names, and even attribute values when
it does not make a difference, in lowercase. The probability of a lowercase
character in written language is already higher than the probability of an
uppercase character (and so the overall redundancy of the former is
greater), and this measure takes advantage of that.
However, a few extra characters here and there for the sake of legibility
aren't likely to matter.

ACK


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <9760703e-bf01-4192-954b-1ed9ddc26f54@y5
g2000hsf.googlegroups.com>, Tue, 22 Jan 2008 18:44:34, RobG
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.

It's not only in transmission that file sizes can matter; the non-zipped
size matters to me.

I once, after a few years of manual page-editing, applied a tool that
removed all trailing whitespace from .htm files. The reduction was of
the order of a percent of file size, which is a significant proportion
of my slack space.

My own *.js files are clear of trailing whitespace, but one from NASA
has that as a lesser fault.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top