Just wondering if anyone has any thoughts on performance in terms of dynamic variable name assignment.  We have been having some discussions here at work, and I did some searching for the performance difference between using string variable assignment and brackets for creating dynamically named variables. 

    For example, what is better:

<cfset "variables.datafields.#field#.value" = arguments.value />

    Or, this:

<cfset variables.datafields[field].value = arguments.value />

When researching the topic, I ran across the page entitled Using pound signs to construct a variable name in assignments on the livedocs web site (for CF 6.1, but probably applied to CF 7.x and maybe even the upcoming release of CF 8 as well).  According to the article, it states that:

You can combine text and variable names to construct a variable name on the left side of a cfset assignment. For example, the following code sets the value of the variable Product12 to the string "Widget":

<cfset ProdNo = 12><cfset "Product#ProdNo#" = "Widget">

To construct a variable name this way, all the text on the left side of the equal sign must be in quotation marks.

This usage is less efficient than using arrays. The following example has the same purpose as the previous one, but requires less processing:

<cfset MyArray=ArrayNew(1)> <cfset prodNo = 12> <cfset myArray[prodNo] = "Widget"> 

I believe that using an array or struct with bracket notation is best, but wondered if anyone had any ideas, thoughts, or better yet some experience on which one has better performance.

This entry was posted on Thursday, July 19th, 2007 at 10:52 am.
Categories: Uncategorized.

No Comments, Comment or Ping

Reply to “Dynamic variable names in CF”