title: Are JavaScript Arrays actually implemented as arrays?
body: <p>The difference between a JavaScript <code>Array</code>, and <code>Object</code> is not very big. In fact it seems <code>Array</code> mainly adds the <code>length</code> field, so you can use both <code>Array</code>s and <code>Object</code>s as numeric arrays:</p>
  <pre><code>var ar = new Array();
  ar[0] = &quot;foo&quot;;
  ar[&quot;bar&quot;] = &quot;foo&quot;;
  
  var ob = new Object();
  ob[0] = &quot;foo&quot;;
  ob[&quot;bar&quot;] = &quot;foo&quot;;
  
  assert(ar[0] == ob[0] == ar[&quot;0&quot;] == ob[&quot;0&quot;] == ar.bar == ob.bar); // Should be true.
  </code></pre>
  <p>So my questions is, <em>in popular JavaScript engines</em> (<a href="https://en.wikipedia.org/wiki/V8_%28JavaScript_engine%29" rel="nofollow noreferrer">V8</a>, JavaScriptCore, <a href="https://en.wikipedia.org/wiki/SpiderMonkey" rel="nofollow noreferrer">SpiderMonkey</a>, etc.), how is this handled? Obviously we do not want our arrays to be actually stored as hash maps with key values! How can we be reasonably sure our data is stored as an actual array?</p>
  <p>As far as I can see there are a few approaches engines could take:</p>
  <ol>
  <li><code>Array</code> is implemented exactly the same way as <code>Object</code> - as an associative array with string keys.</li>
  <li><code>Array</code> is a special case, with a <code>std::vector</code>-like array backing the numeric keys, and some density heuristic to prevent insane memory use if you do <code>ar[100000000] = 0;</code></li>
  <li><code>Array</code> is the same as <code>Object</code>, and all objects get a heuristic to see if using an array would make more sense.</li>
  <li>Something insanely complicated that I haven't thought of.</li>
  </ol>
  <p>Really this would be simpler if there were a proper array type (<em>cough</em> <a href="https://en.wikipedia.org/wiki/WebGL" rel="nofollow noreferrer">WebGL</a> typed arrays <em>cough</em>).</p>