Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <pre><code>// 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 </code></pre> <p>But, passing in one argument results differently:</p> <pre><code>// 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 </code></pre> <hr> <p>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.</p> <p>I did a <a href="http://jsfiddle.net/pLyH5/" rel="nofollow noreferrer"><strong>speed test</strong></a> in Chrome 6, in which I defined 20 times 10000000 the same array <code>1, 2, 3</code>, which gave these results:</p> <pre><code>Average speed per 10000000 calls Array Constructor : 226.55 ms Array Literal : 159.1 ms </code></pre> <p>As you can see, the array literal is 67,45ms faster per 10000000 array definitions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload