Home Dashboard Directory Help
Search

[Bug] new Array() by Internet Explorer User


Status: 

Resolved
 as By Design Help for as By Design


Type: Bug
ID: 836640
Opened: 3/19/2014 3:59:41 AM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

Steps:
1. Open F12 Developer Tools
2. Go to Console
3. Enter "new Array()" (without quotes)
4. Notice that the result is an empty array
5. Enter "new Array(2)" (without quotes)
6. Notice that the result is "[undefined, undefined]"

Entering "new Array(X)" in the Console (where X is any natural number) should ALWAYS result as an empty array (with the length of X), not an array full of "undefined" values.

Please fix this bug.
Details
Sign in to post a comment.
Posted by Microsoft on 7/14/2014 at 11:48 AM
Hello!
This behavior is by design. When you create an Array by calling the constructor with a predetermined length, you actually create a dense array with the specified number of slots. That the slots do not contain anything (therefore containing undefined) is a side effect; the following code snippets are equivalent:

var x = new Array(2);

var y = [undefined, undefined];

var z = [,];




However, this is not semantically equivalent:

var w = [];

w.push(undefined);

w.push(undefined);




In the original cases, the arrays have been preallocated with some number of slots. While arrays may grow, those slots are there. In the latter case, you have grown the array. While JavaScript arrays are able to grow without copying memory all of the time, if you know the size of the array that you're going to create, it is more efficient to create an array of the correct size ahead-of-time.

Best Regards,
The Internet Explorer Team
Posted by Microsoft on 3/20/2014 at 6:10 AM
Thank you for your feedback!

We will be investigating this issue further.

Best Regards,
The Internet Explorer Team
Sign in to post a workaround.