As you already know, your test codes were wrong. Here is an example jsperf test that uses assignment in all methods.
The speed is the same for all of them in Firefox 6 (~ 50,000 op / s). However, in Chrome 13, setting the length of the array in advance leads to a huge increase in speed (~ 80,000 op / s versus ~ 400,000 op / s). To find the reason for this, you would need to take a look at the source code of the Chrome JavaScript engine.
You asked what happens on .push() and new Array . It is necessary to describe the specification:
The internal [[Prototype]] property for the newly created object is set to the original Array prototype object, one that is the initial value of Array.prototype ( 15.4.3. 1). The internal [[Class]] property for the newly created object is " Array ". The internal [[Extensible]] property of the newly created object is true .
If the argument of len is Number and ToUint32(len) is equal to len, then the length property for the newly created object is set to ToUint32(len) . If the argument of len is Number and ToUint32(len) not equal to len, a RangeError exception is thrown .
If len is not a number, then the length property of the newly created object is set to 1, and the 0 property of the newly created object is set to len with the attributes {[[Writable]]: true, [[Enumerable]]: true, [[Configurable] ]: true} ..
Arguments are added to the end of the array in the order in which they are displayed. The new array length is returned as a result of the call.
When the push method is called with zero or more arguments item1, item2, etc., the following steps are performed:
- Let O be the result of calling ToObject, passing this value as an argument.
- Let lenVal be the result of calling the [[Get]] O internal method with the argument " length ".
- Let n be ToUint32 (lenVal).
- Let the elements be an internal list, the elements of which are in order from left to right, the arguments that were passed to this function call.
- Repeat while items are not empty
- Remove the first element from the elements and let E be the value of the element.
- Call the internal method O [[Put]] with arguments ToString (n), E, ββand true .
- Increase n by 1.
- Call the internal method O [[Put]] with arguments length ", n and true .
- Returns n.
However, the implementation may be different. As you saw from the difference in Firefox and Chrome, Chrome seems to internally optimize the structure of the array.
Felix kling
source share