Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new Array() vs []

4 views
Skip to first unread message

Laurent vilday

unread,
Sep 5, 2006, 4:38:22 AM9/5/06
to
Hello, one is pretending it is /better/ to use new Array() and *never*
use the literal notation [], is he wrong or right ? If he is wrong, as i
suspect he is, how can i explain him, with simple terms :) he is wrong ?

http://javascript.crockford.com/code.html
Here in "Bonus Suggestions" it is write :

"Use {} instead of new Object(). Use [] instead of new Array()."

I've seen it write in a lot of ressources, but never with any concrete
explanation, so why ?

Thanks.

--
laurent

Cameron McCormack

unread,
Sep 5, 2006, 5:53:45 AM9/5/06
to
Hi Laurent.

Laurent vilday wrote:
> Hello, one is pretending it is /better/ to use new Array() and *never*
> use the literal notation [], is he wrong or right ? If he is wrong, as i
> suspect he is, how can i explain him, with simple terms :) he is wrong ?

Better in what way?

ECMA-262 says (11.1.4):

The production ArrayLiteral: [ Elision(opt) ] is evaluated as follows:

1. Create a new array as if by the expression new Array().
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Put]] method of Result(1) with arguments "length" and
Result(2).
4. Return Result(1).

And from (15.4.2.1) you can see that new Array() would set "length" to 0
anyway. So they are just the same in terms of behaviour.

[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There's no reason not to use it.

--
Cameron McCormack, http://mcc.id.au/
xmpp:hey...@jabber.org ▪ ICQ 26955922 ▪ MSN c...@mcc.id.au

Matt Kruse

unread,
Sep 5, 2006, 8:18:15 AM9/5/06
to
Cameron McCormack wrote:
> [] is shorter to type, and would be quicker to parse (assuming a
> reasonable implementation). There's no reason not to use it.

Ancient browsers didn't support it.
I don't even remember which versions of Netscape - 3.0, perhaps? Or even
2.x?
Not using [] and {} may be a recommendation from someone who came to the
conclusion long ago an just hasn't changed it in modern times.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


peterm...@gmail.com

unread,
Sep 5, 2006, 7:08:43 PM9/5/06
to
Matt Kruse wrote:
> Cameron McCormack wrote:
> > [] is shorter to type, and would be quicker to parse (assuming a
> > reasonable implementation). There's no reason not to use it.
>
> Ancient browsers didn't support it.
> I don't even remember which versions of Netscape - 3.0, perhaps? Or even
> 2.x?
> Not using [] and {} may be a recommendation from someone who came to the
> conclusion long ago an just hasn't changed it in modern times.

<URL: http://jslint.com >

INPUT
var x = new Array();

OUTPUT
Problem at line 1 character 13: Use the array literal notation [].

VK

unread,
Sep 6, 2006, 2:18:12 AM9/6/06
to

peterm...@gmail.com wrote:
> INPUT
> var x = new Array();
>
> OUTPUT
> Problem at line 1 character 13: Use the array literal notation [].

JSLint is a great tool with a lot of thoughts put into it, but it has
some highly specific ideas of the proper programming and syntax, so use
with caution :-)

new Array(length) can be used to instantiate an array of a given
initial length without populating it. Unless you have such intention,
squared brackets way could be used.

Yet this issue has relevance to "cool looking vs. lame looking" issue
rather then to the "good programming vs. bad programming" one :-)

Dr John Stockton

unread,
Sep 6, 2006, 6:32:20 PM9/6/06
to
JRS: In article <1157523492....@m73g2000cwd.googlegroups.com>,
dated Tue, 5 Sep 2006 23:18:12 remote, seen in
news:comp.lang.javascript, VK <school...@yahoo.com> posted :

>new Array(length) can be used to instantiate an array of a given
>initial length without populating it. Unless you have such intention,
>squared brackets way could be used.


Indeed. But when is it useful to do so?

ISTR there's a related discussion, involving speed, in news:fr.c.l.j -
perhaps a true francophone might report any conclusions?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

VK

unread,
Sep 7, 2006, 9:04:36 AM9/7/06
to
> >new Array(length) can be used to instantiate an array of a given
> >initial length without populating it. Unless you have such intention,
> >squared brackets way could be used.
>
> Indeed. But when is it useful to do so?

This is the question of a kind "what blue paint can be used for?" You
know for sure that it can be used for many things, but try to give a
sample right away :-)

It can be an array where you do not want to exceed certain length or
where you want to take extra steps when the length exceeds certain
limits.

Say (from my most recent cases) it can be a license limitation for no
more than 10 simultaneous requests to the server. It can be say 1000
records array and then you need to alert user about something (or save,
or refuse to add etc.)
Of course - as almost everything in JavaScript - it can be made in
different ways:

a)
var arr = new Array(1000);
...
if (i > arr.length) {
// do something
}
...

b)
var arr = [];
var uBound = 1000;
...
if (i > uBound) {
// do something
}
...

c)
var arr = [];
arr.uBound = 1000;
...
if (i > arr.uBound) {
// do something
}
...

d) and so on.

But sometimes and rather often the simpliest way is the best one ;-)


> ISTR there's a related discussion, involving speed, in news:fr.c.l.j -
> perhaps a true francophone might report any conclusions?

JavaScript doesn't allocate/reallocate memory for undefined array
members. So there is no difference between new Array() and [] in
productivity or memory usage. There is nothing to discuss here neither
in French, nor in English - though I read the suggested thread.

ASM

unread,
Sep 7, 2006, 12:06:15 PM9/7/06
to
VK a écrit :
>>> new Array(length)
[...]

>> ISTR there's a related discussion, involving speed, in news:fr.c.l.j -
>> perhaps a true francophone might report any conclusions?
>
> JavaScript doesn't allocate/reallocate memory for undefined array
> members. So there is no difference between new Array() and [] in
> productivity or memory usage. There is nothing to discuss here neither
> in French, nor in English - though I read the suggested thread.

But probably Firefox doesn't do the job as other browsers ... ?
Answering diffently when you loop a [...] using method for(i in t)

t = new Array();
t[0] = "A";
t[3] = "B";

= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 = Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==> A undefined undefined B
for( i in t ) ==> A B


t = ['A',,,'B'];

= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
for(i=0; i<t.length; i++) ==> A undefined undefined B
for( i in t ) ==> A B

= Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==> A undefined undefined B
for( i in t ) ==> A undefined undefined B

<http://groups.google.com/group/fr.comp.lang.javascript/msg/3aa5c83edea38f50?rnum=1>

--
Stephane Moriaux et son [moins] vieux Mac

Cameron McCormack

unread,
Sep 8, 2006, 2:14:46 AM9/8/06
to
ASM wrote:
> t = ['A',,,'B'];
>
> = IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
> for(i=0; i<t.length; i++) ==> A undefined undefined B
> for( i in t ) ==> A B
>
> = Fx 1.5.0.6 =
> for(i=0; i<t.length; i++) ==> A undefined undefined B
> for( i in t ) ==> A undefined undefined B

Firefox's behaviour is incorrect here (according to ECMA-262 11.1.4.)

yueli...@gmail.com

unread,
Sep 8, 2006, 2:34:57 AM9/8/06
to
I never notice this, thank you very much.

RobG

unread,
Sep 8, 2006, 3:06:09 AM9/8/06
to

yueli...@gmail.com wrote:
> I never notice this, thank you very much.

Please don't top post here.

You probably didn't notice because it is extremely unlikely that anyone
would use an initialiser for a sparse array with an elision large
enough to notice the difference in speed between a for..in and a for
loop. It requires about 400 elements to be measurable at all on my
modest PC, and 10 times that to be noticeable:

var x = [],
i = 400;
while (i--){ x[i] = ',' }
eval('var y = ["A",' + x.join('') + '"B"];');

var startTime = new Date();
for (var x in y){ y[x] }
var endTime = new Date();
alert(y.length + ' elements took '
+ (endTime.getTime()-startTime.getTime()) + 'ms'
);


> Cameron McCormack wrote:
> > ASM wrote:
> > > t = ['A',,,'B'];
> > >
> > > = IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
> > > for(i=0; i<t.length; i++) ==> A undefined undefined B
> > > for( i in t ) ==> A B
> > >
> > > = Fx 1.5.0.6 =
> > > for(i=0; i<t.length; i++) ==> A undefined undefined B
> > > for( i in t ) ==> A undefined undefined B
> >
> > Firefox's behaviour is incorrect here (according to ECMA-262 11.1.4.)

I'd agree with that. No one seems to have logged it as a bug yet, care
to get your name in lights?


--
Rob

Cameron McCormack

unread,
Sep 8, 2006, 4:23:50 AM9/8/06
to
RobG wrote:
> I'd agree with that. No one seems to have logged it as a bug yet, care
> to get your name in lights?

Looking through the SpiderMonkey source, it seems this bug has already
been filed (a couple of years ago):

https://bugzilla.mozilla.org/show_bug.cgi?id=260106

0 new messages