Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you have been already made aware of, your testcases were flawed. <a href="http://jsperf.com/array-push-comparision" rel="nofollow">Here is a jsperf test</a> case which uses assignment in all methods. </p> <p>The speed is the same for all of them in Firefox 6 (~50,000 op/s). In Chrome 13 however, setting the array length beforehand results in a huge speed improvement (~80,000 op/s vs. ~400.000 op/s). To find the reason for that, one would have to have a look at the source code of Chrome's JavaScript engine.</p> <p>You asked what happens on <code>.push()</code> and <code>new Array</code>. The specification describes was <em>should</em> happen:</p> <blockquote> <h3><a href="http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.2.2" rel="nofollow">15.4.2.2 new Array (len)</a></h3> <p>The [[Prototype]] internal property of the newly constructed object is set to the original Array prototype object, the one that is the initial value of <strong>Array.prototype</strong> (<a href="http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.3.1" rel="nofollow">15.4.3.</a>1). The [[Class]] internal property of the newly constructed object is set to "<strong>Array</strong>". The [[Extensible]] internal property of the newly constructed object is set to <strong>true</strong>.</p> <p>If the argument <em>len</em> is a Number and <code>ToUint32(len)</code> is equal to <em>len</em>, then the length property of the newly constructed object is set to <code>ToUint32(len)</code>. If the argument <em>len</em> is a Number and <code>ToUint32(len)</code> is not equal to <em>len</em>, a <strong>RangeError</strong> exception is thrown.</p> <p>If the argument <em>len</em> is not a Number, then the length property of the newly constructed object is set to 1 and the 0 property of the newly constructed object is set to <em>len</em> with attributes {[[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}.. </p> <h3><a href="http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.3.1" rel="nofollow">15.4.4.7 Array.prototype.push ( [ item1 [ , item2 [ , … ] ] ] )</a></h3> <p>The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call.</p> <p>When the push method is called with zero or more arguments item1, item2, etc., the following steps are taken:</p> <ol> <li>Let <em>O</em> be the result of calling ToObject passing the this value as the argument.</li> <li>Let <em>lenVal</em> be the result of calling the [[Get]] internal method of <em>O</em> with argument "<strong>length</strong>".</li> <li>Let <em>n</em> be ToUint32(<em>lenVal</em>).</li> <li>Let <em>items</em> be an internal List whose elements are, in left to right order, the arguments that were passed to this function invocation.</li> <li>Repeat, while <em>items</em> is not empty <ol> <li>Remove the first element from <em>items</em> and let <em>E</em> be the value of the element.</li> <li>Call the [[Put]] internal method of <em>O</em> with arguments ToString(<em>n</em>), <em>E</em>, and <strong>true</strong>.</li> <li>Increase <em>n</em> by 1.</li> </ol></li> <li>Call the [[Put]] internal method of O with arguments "<strong>length</strong>", <em>n</em>, and <strong>true</strong>.</li> <li>Return <em>n</em>.</li> </ol> </blockquote> <p>That said, the implementation can be different. As you saw from the difference in Firefox and Chrome, Chrome seems to optimize the array structure internally.</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. This table or related slice is empty.
    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