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

new Array() vs []

P: n/a
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
Sep 5 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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:he****@jabber.org ▪ ICQ 26955922 ▪ MSN ca*@mcc.id.au
Sep 5 '06 #2

P: n/a
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
Sep 5 '06 #3

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

Sep 5 '06 #4

P: n/a
VK

pe**********@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 :-)

Sep 6 '06 #5

P: n/a
JRS: In article <11*********************@m73g2000cwd.googlegroups. com>,
dated Tue, 5 Sep 2006 23:18:12 remote, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.composted :
>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.zipTimo Salmi's Turbo Pascal FAQ.
Sep 6 '06 #6

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

Sep 7 '06 #7

P: n/a
ASM
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
Sep 7 '06 #8

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

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org ▪ ICQ 26955922 ▪ MSN ca*@mcc.id.au
Sep 8 '06 #9

P: n/a
I never notice this, thank you very much.
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.)

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org ▪ ICQ 26955922 ▪ MSN ca*@mcc.id.au
Sep 8 '06 #10

P: n/a

yu********@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

Sep 8 '06 #11

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

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org ▪ ICQ 26955922 ▪ MSN ca*@mcc.id.au
Sep 8 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.