title: Is reading the `length` property of an array really that expensive an operation in JavaScript?
body: <p>I always assumed caching the length of an array in JavaScript is a good idea (especially in the condition of a <code>for</code> loop) because of the expensiveness of calculating the length of an array.</p>
  <h3>Example</h3>
  <pre><code>for (var i = 0; i &lt; arr.length; i++) { }
  
  // vs
  
  for (var i = 0, arrLength = arr.length; i &lt; arrLength; i++) { }
  </code></pre>
  <p>However, I thought perhaps the <code>length</code> property is only updated on creation and alteration of the array. Therefore, reading it shouldn't be too expensive an operation as opposed to reading it stored in a variable (as opposed to other methods in other languages that may need to seek in memory to find the end of something, e.g. <code>strlen()</code> in C).</p>
  <p>I have two questions. I am also interested in how this works, so please don't hit me with the <em>premature optimisation</em> stick.</p>
  <p>Assume the JavaScript engines in browsers.</p>
  <ol>
  <li>Is there any advantage to caching the <code>length</code> property of an array in JavaScript? Is there much more involved in reading a local variable over an object's property?</li>
  <li>Is the <code>length</code> property simply altered on creation and on <code>shift()</code> and <code>pop()</code> type methods that don't return a new array and otherwise simply stored as an integer?</li>
  </ol>