I always assumed caching the length of an array in JavaScript is a good idea (especially in the condition of a for
loop) because of the expensiveness of calculating the length of an array.
for (var i = 0; i < arr.length; i++) { } // vs for (var i = 0, arrLength = arr.length; i < arrLength; i++) { }
However, I thought perhaps the length
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. strlen()
in C).
I have two questions. I am also interested in how this works, so please don't hit me with the premature optimisation stick.
Assume the JavaScript engines in browsers.
length
property of an array in JavaScript? Is there much more involved in reading a local variable over an object's property?length
property simply altered on creation and on shift()
and pop()
type methods that don't return a new array and otherwise simply stored as an integer?The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
Summary. The length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index of the array. The length returns the number of elements that a dense array has.
When you extend an array by changing its length property, the number of actual elements does not increase; for example, if you set length to 3 when it is currently 2, the array still contains only 2 elements. Thus, the length property does not necessarily indicate the number of defined values in the array.
length is a property of array, not a method if it was a method getting the length of an array would be of O(n), but by keeping it as a property its O(1).
Well, I would have said it was expensive, but then I wrote a little test @ jsperf.com and to my surprise using i<array.length
actually was faster in Chrome, and in FF(4) it didn't matter.
My suspicion is that length is stored as an integer (Uint32). From the ECMA-specs (262 ed. 5, page 121):
Every Array object has a length property whose value is always a nonnegative integer less than 232. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes
Phew! I don't know if I ever get used to such language ...
Finally, we always have our good old lagging behind browser. In IE (9, 8, 7) caching the length is really faster. One of many more reasons to not use IE, I say.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With