I understand that some Objects use classes as their underlying data structure. If there are a lot of properties, it is sometimes treated as a hash table?
I also understand that Arrays are sometimes treated like C++ Arrays (i.e. fast random indexing, slow deletion and resizing). And, other times, they are treated more like Objects (fast indexing, fast insertion/removal, more memory). And, maybe sometimes they are stored as linked lists (i.e. slow random indexing, fast removal/insertion at the beginning/end)
More specifically, what it the performance impact of:
var arr = ;
arr = 20;
var arr = ;
//Add lots of items
for(var i = 0; i < 1000000; i++)
arr[i] = Math.random();
//Now I use it like a queue...
for(var i = 0; i < arr.length; i++)
var item = arr[i].shift();
//Do something with item...
(i have saved a copy of the test case, and will update the answer once JSPref is fixed / a successor is found)
Hmm... maybe an overkill for the answer... but I created a test suite, precisely to explore these issues (and more) (archived copy).
And in that sense, you can see the performance issues in this 50+ test case tester (it will take a long time).
Also as its name suggest, it explores the usage of using the native linked list nature of the DOM structure.
The summary is as followed
Array.shift()is fast ~approx 6x slower than an array pop, but is ~approx 100x faster than an object attribute deletion.
Array.push( data );is faster than
Array[nextIndex] = databy almost 20 (dynamic array) to 10 (fixed array) times over.
Array.unshift(data)is slower as expected, and is ~approx 5x slower than a new property adding.
array[index] = nullis faster than deleting it
delete array[index](undefined) in an array by ~approx 4x++ faster.
obj[attr] = null~approx 2x slower than just deleting the attribute
Array.splice(index,0,data)is slow, very slow.
Array.splice(index,1,data)has been optimized (no length change) and is 100x faster than just splice
dll.splice(index,1)removal (Where it broke the test system).
Note: These metrics applies only to large array/objects which v8 does not "entirely optimise out". There can be very isolated optimised performance cases for array/object size less then an arbitrary size (24?). More details can be seen extensively across several google IO videos.
Note 2: These wonderful performance results are not shared across browsers, especially
*cough* IE. Also the test is huge, hence i yet to fully analyze and evaluate the results : please edit it in =)
Updated Note (dec 2012): Google representatives have videos on youtubes describing the inner workings of chrome itself (like when it switches from a linkedlist array to a fixed array, etc), and how to optimize them. See GDC 2012: From Console to Chrome for more.
Updated Note (feb 2013): Thx @badunk, for providing the video link at the exact point