Friday, January 1, 2016

Scoped variable performance: form

In light of the speed penalty for scoping your query variables, and my recent foray into the form object, I started to wonder: Does explicitly scoping other variables have the same performance penalty?

tldr: Explicitly specifying the form scope is faster than leaving it implicit. This is the reverse of the query scope's behavior.

Here's the test code:

 <cfset form.item1=1>  
 <cfset form.item2=2>  
 <cfsilent>  
 <cftimer label="unscoped">  
     <cfloop index="i" from=1 to=1000000>  
         #item1# #item2#  
     </cfloop>  
 </cftimer>  
 <cftimer label="scoped">  
     <cfloop index="i" from=1 to=1000000>  
         #form.item1# #form.item2#  
     </cfloop>  
 </cftimer>  
 </cfsilent>  
Result: unscoped took 900ms to scoped's 560ms. The gap varied, but accessing form directly was always faster.

Now, don't go rushing to change all your old code. It took me 2 million accesses to make a difference of half a second. This is not going to be a drain on your application unless you're inside a very troublesome loop.

For what it's worth, I also ran the same test against url instead of form, with the same result. The query scope appears to be unique in its sloth.

No comments:

Post a Comment