Performance - Calculating Length in loops


does script calculate length of an array every cycle?

For example in a loop

for (int i = 0; i < ArrayName.Length; i++)

is “ArrayName.Length” calculated every iteration?

From Google’s JavaScript styling, I noticed they use

for( var i = 0, ii = array.length; i < ii; i++) {
// some stuff here

So I assume that, it calculates array length each iteration, which is actually a performance loss.
I am not sure if it is the same for C#, but considering the syntax, it pretty much calls the array length each iteration (maybe it holds that somewhere and does not count it each time, I don’t know, Unity’s compiler is a mystery to me, and it uses .Net 2.0 which is pretty old).

By convention, I do not call array length each iteration but I do not see any performance difference in my work either.

ArrayName.length is looked up every iteration. But due to the fixed length nature of arrays it’s not calculate every time.

This is only valid so long as you don’t resize the array in your loop.

For other reasons it’s generally considered bad practice to add or remove an item from a collection while iterating over it.