Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of JavaScript new Array(n) Declaration

Tags:

javascript

Basic JavaScript question: Since there is no hard limit for arrays as the case with Java (i.e. IndexOutOfBoundsException), what is the use of the declaration where we specify the length property?

var a = new Array(10); 

I know it predefines the length and puts "undefined" into those empty spots. Is that reason enough for having it?

like image 249
OpenSource Avatar asked Aug 07 '09 18:08

OpenSource


People also ask

What is the use of new in array?

new is used for allocating dynamic objects (which can grow like, ArrayList ), but arrays are static (can't grow). So one of them is unnecessary: the new or the size of the array.

What does new array do in JavaScript?

The call to new Array(number) creates an array with the given length, but without elements. The length property is the array length or, to be precise, its last numeric index plus one.

How do you make an array with N values?

To create an array with N elements containing the same value: Use the Array() constructor to create an array of N elements. Use the fill() method to fill the array with a specific value. The fill method changes all elements in the array to the supplied value.

What is the difference between new array and [] JavaScript?

They are fundamentally similar. [] is pretty much shortcut for new Array() , so it is preferable. The former is called an array literal/array shorthand. The latter is an example of using the array constructor, which really does the same thing (the array constructor gets called behind the scenes for the literal).


2 Answers

There are many perceived benefits of declaring an array size, but I think the majority of the perceived benefits are just FUD being passed around.

Better performance!/It's faster!

As far as I can tell the difference between pre-allocating and dynamic allocation is negligible.

More interestingly, the spec does not state that the array should be set to a pre-allocated length!

From Section 15.4.2.2 ECMA-262:

If the argument len is a Number and ToUint32(len) is equal to len, then the length property of the newly constructed object is set to ToUint32(len). If the argument len is a Number and ToUint32(len) is not equal to len, a RangeError exception is thrown.

An unscientific for-fun test case is here: http://jsbin.com/izini

It makes for more understandable code!

Personally, I disagree.

Consider the javascript you have written in the past, and consider code you may have to write in the future. I can't think of a single time where I've needed to specify a static limit on one of my arrays. I'd also argue that the potential problems of limiting arrays in javascript highly outweigh the benefits caused by letting people know what you were thinking with no actual checks behind it. Lets weigh the pros and cons...

Pros:

  1. It will be easier for them to understand what you intended the code to do.
  2. They will be able to find the bugs caused by your assumption later on (tongue firmly in cheek)

Cons:

  1. Quick glances can easily confuse "new Array(10)" with "new Array('10')" which do entirely different things!
  2. You are imposing an arbitrary limit on code with no normal length limit causing you to write lots of boiler plate code to check and maintain the limit.
  3. You are imposing an arbitrary limit on code which could probably have been generalized to work with any length of values.
  4. You're making an assumption about how people will read your code while assuming that the alternative would be less confusing.

You may as well have written:

//I assume this array will always be length 10 var arr = new Array(); 

In the above case the comment might even be preferable. The explicit declaration of intent can avoid any confusion not used to using the constructor as a declaration of intent.

Fine then.. why do you think it's even there then?

Convenience. When they were writing the spec I think they realized two things.

  1. This sort of assignment would be something developers coming from similar languages would be used to.
  2. Implementations of ECMAScript might potentially use it for performance gains.

So they put it in there. The spec only defines the use of the parameter, not how it should be implemented.

like image 154
coderjoe Avatar answered Oct 04 '22 20:10

coderjoe


Performance on the V8 JavaScript engine.

By doing:

var arr = []; arr.length = 1000; 

V8 will preallocate the required memory for the array and maintain/set the array's Hidden Class to compact SMI (Small Int, 31 bits unsigned) array. However, this is not true when the desired length is too big, which results in the HC being set to sparse array (i.e., map).

Try the following link on Chrome: http://jsperf.com/0-fill-n-size-array

I've included an extra test case without the array length definition so you can tell the actual performance difference.

Related info: http://www.youtube.com/watch?v=UJPdhx5zTaw

like image 26
Gabriel Garcia Avatar answered Oct 04 '22 22:10

Gabriel Garcia