JavaScript Arrays

12 views
Skip to first unread message

Timothy Pote

unread,
Jul 4, 2013, 1:09:12 PM7/4/13
to nashvi...@googlegroups.com
There were a number of questions on JavaScript Arrays during the presentation, and unfortunately I didn't have all the answers at the time.  However, you're now in luck!  Because I've done a bit of research and gotten some answers for you.

Arrays vs Objects

JavaScript arrays are indeed just objects: bags of name-value pairs.  So in the following example array and arrayObject are essentially the same thing:

var array = [ 'zero', 'one', 'two', 'three' ];

var arrayObject = { 
  '0': 'zero',
  '1': 'one',
  '2': 'two',
  '3': 'three'
};

There are only two major differences between the two.  One is the fact that array's [[prototype]] is Array.prototype, giving it access to all kinds of useful methods like pushslice, and join.  The other difference is the fact that array is given access to the length property: a special property that is equal to the largest integer property of the array + 1.  It does not indicate the number of items in the array, and it does not indicate the capacity of the array (as in Java).  For example:

var array = [];
array[100] = '';
array.length // 101

As mentioned in the talk, array.length can be set explicitly.  Making it larger does nothing.  Making it smaller truncates all properties with an integer value greater than or equal to the new value of array.length:

var array = [ 'zero', 'one', 'two' ];
array.length = 1;
array // [ 'zero' ]

This of course leads to the question: Why didn't array.0 work during the talk?

In my nervousness, I completely forgot that legal JS identifiers cannot start with digits.  Which is why you must access array elements with the bracket syntax.  (I forgot to mention in my talk that the bracket syntax allows you to use whatever characters you like for property names, whereas the dot syntax requires you to use legal JS identifiers.)  For those of you interested in legal JS identifiers StackOverflow has a good discussion on it.

new Array() vs []

This one has been hashed out many times on StackOverflow, but the general consensus is that [] is the preferred syntax.  Technically the Array function can be redefined at any time (not that any of you should ever do that), so using the literal syntax guarantees that you'll get a real array.  Also, as mentioned in the talk, the Array constructor function behaves differently if you pass it a single integer, which is quite strange.

var arrayOne = new Array('one');
arrayOne // [ 'one' ]

var array1 = new Array(1);
array1 // []
array1.length // 1

Array Performance

According to this dude there are indeed optimizations the JS engine can make if you use an array instead of a regular object, as long as you don't try to treat the array as a regular object by adding non-integer properties to it.

Csaba Toth

unread,
Jul 4, 2013, 9:59:05 PM7/4/13
to nashvi...@googlegroups.com
Such corner cases remind me the funniest lightning talk I've ever watched. It's from CodeMash and there are Ruby and JS corner cases in it. I was literally laughing out loud at the sarcasm when I was watching it.
https://www.destroyallsoftware.com/talks/wat

Csaba


--
You received this message because you are subscribed to the Google Groups "Nashville Java User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nashville-ju...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages