8

Is it better to write

var arr=[]; then var arr=new Array(); 
var obj={}; then var obj=new Object();

and if so, why?

I read slide lection page 36 about that idea, but no explanation was given or example why its better.

Ben
  • 25,389
  • 34
  • 109
  • 165

3 Answers3

8

There is not a big difference between these definitions, except that the first way uses the array/object literal and the second the array/object constructor.

The array constructor may return different results, depending on the number of arguments passed in. If you pass in one argument, a new empty array is created of the length of that argument. For example:

// arr1 is the same as arr2
var arr1 = new Array(1, 2, 3, 4);
var arr2 = [1, 2, 3, 4];

alert(arr1.length == arr2.length); // true
alert(arr1[0]); // 1
alert(arr2[0]); // 1

But, passing in one argument results differently:

// arr3 has length 200 and is empty, while arr4 has length 1 and contains a number
var arr3 = new Array(200);
var arr4 = [200];

alert(arr3.length == arr4.length); // false
alert(arr3[0]); // 'undefined'
alert(arr4[0]); // 200

The speediest way to define an array or object is of course the literal way, because you don't need to call the constructor first. Anyway, the actual speed difference is negligible, really.

I did a speed test in Chrome 6, in which I defined 20 times 10000000 the same array 1, 2, 3, which gave these results:

Average speed per 10000000 calls
Array Constructor  : 226.55 ms
Array Literal      : 159.1  ms

As you can see, the array literal is 67,45ms faster per 10000000 array definitions.

Harmen
  • 22,092
  • 4
  • 54
  • 76
  • the question also here what more fast for computer to run – Ben Aug 02 '10 at 08:44
  • while it is an interesting point that the `Array` constructor treats the arguments differently based on how many there are, i think it is completely misleading to list that as a difference in a comparison between the two ways of creating an array. `[]` is not a method that you invoke with arguments, any more than `var a = "test"` is four arguments passed to a `""`-function. it is nothing more than a syntax for creating arrays, that only implicitly deals with assigning dimensions. – David Hedlund Aug 02 '10 at 09:15
  • 3
    http://jsperf.com/literal-vs-constructor-array – James Aug 02 '10 at 09:27
  • @J-P Great testpage; operations/time is better than time/operation... My test fails on Firefox because I get this "stop script" popup. – Harmen Aug 02 '10 at 09:30
1

thomas fuchs says in his slideshow, under that video (the part saying "embrace the language" on page 20): var arr=[] and var obj={} is better and slightly faster. I'm not sure why, but anyway, it's a pretty interesting slideshow :)

koko
  • 958
  • 12
  • 26
1

From a typical programmer perspective, it seems that you tend to use var arr = new Array(); so as to give the feeling of typical object instantiation. But in javascript its usually good practice to use var arr = []; Personally I dont see much difference in using both ways except for one parameter which is explained by Harmen.

Check this link too - http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103