You're right, this is very contrived and is an extreme case, but I agree in such an edge case you would not want to create an additional struct (even though I would argue that the performance impact is not very great when you're already creating hundreds of thousands of separate String instances for the var decalred variables.) I've built a lot of large applications have have rarely run into a situation where I need to execute a loop even 1000 times, let alone 100,000 times. In the vast majority of cases, using a local struct has extremely limited impact on performance. And for me, the benefit of seeing that every local variable is truly local is what I am really after anyway. It's so easy for people to forget to var scope everything that in my opinion this performance impact outweighs the danger of variables slipping into the variables scope.
Regards,
Brian
The problem with the "local = StructNew()" method is there are times when it can cause problems. Take this code for example:
<cffunction name="addNumbers" result="numeric">
<cfargument name="num1" type="numeric">
<cfargument name="num2" type="numeric">
<cfset var local = StructNew()>
<cfset local.result = num1 + num2>
<cfreturn local.result>
</cffunction>
Now yes, this is contrived, and I know I could return the result directly – it's just for the sake of the example. Now pretend you have some code that calls this in a loop:
<cfloop from="1" to="100000" index="i">
<cfset mySum = addNumbers(i, 100)>
</cfloop>
Well guess what – you just added the overhead of 100,000 unnecessary object creations. You've also added 100,000 StructInsert() calls (the cfset local.result statement does it implicitly). Not only does this directly hurt the performance of the code, it also affects the underlying server processes. That's now 100,000 additional structs that have to be Garbage Collected!!!
Anyway, in this case, you obviously would never want to use the local = StructNew() method, and I run into too many other examples of this type of code that make it undesirable to use. Not to mention I _like_ seeing all of my variables declared up front.
/.02
Roland
