I would be very dubious of the source of your "facts". As often is the case in the CFML world, a little truth in knowledge, that is then taken to the wrong conclusions. Some one blogs about it, then suddenly it becomes folklore! ;)

Right, if you have an array, then the length only gets changed when you add or remove an element to it. This is stored internally to the structure and is _never_ recalculated by traversing the array.


When using loops, there was a time when doing an "ArrayLen(x)" and storing it in a variable and then using that would, would save you a function call; but to be honest, the performance gain you are going to see on that doesn't make a whole lot of difference.

But even an ArrayLen() is just a call to an internal integer.


CFML in this respect is no different from every other language.



Alan Holden wrote:
I've recently learned of a programming requirement, based on the
"fact" that the ColdFusion engine must actually traverse an array -
each time our code requests it's length.

The requirement was that the length of arrays be determined once at
the start (of a method, let's say) and set to a variable instead of
calling arrayLen() over & over again. This would result in less
processor usage.

Picture a x=1; x<array.length; x++ loop. Each time one looped
through the array, CF allegedly had to count the array - with each
iteration of the loop - in which x<array.length was in the looping
condition.

Please discuss. Is the length property computed on the fly each time
it's called? Or does the array object actually store the length
property as a mutable integer that's adjusted only as needed?

Thanks for your wisdom.

Al


--
Open BlueDragon Public Mailing List
http://www.openbluedragon.org/   http://twitter.com/OpenBlueDragon
official manual: http://www.openbluedragon.org/manual/
Ready2Run CFML http://www.openbluedragon.org/openbdjam/

mailing list - http://groups.google.com/group/openbd?hl=en

Reply via email to