title: Overhead when calling a component function vs inline code - ColdFusion
body: <p>I've been diagnosing a performance issue with generating a CSV containing around 50,000 lines and I've narrowed it down to a single function that is used once per line.</p>
  
  <p>After a lot of messing about, I've discovered that there is an overhead in using the function, rather than placing the logic directly in the loop - my question is: Why?!</p>
  
  <p>The function in question is very simple, it accepts a string argument and passes that to a switch/case block containing around 15 options - returning the resulting string.
  I've put a bunch of timers all over the place and discovered that a lot (not all) of the time this function call takes between 0 and 200 ms to run... however if I put the exact same code inline, it sits at 0 on every iteration.</p>
  
  <p>All this points to a fundamental issue in my understanding of object instantiation and I'd appreciate some clarification.</p>
  
  <p>I was always under the impression that if I instantiate a component at the top of a page, or indeed if I instantiate it in a persistent scope like Application or Session, then it would be placed into memory and subsequent calls to functions within that component would be lightning fast. 
  It seems however, that there is an overhead to calling these functions and while we're only talking a few milliseconds, when you have to do that 50,000 times it quickly adds up. </p>
  
  <p>Furthermore, it seems that doing this consumes resources. I'm not particularly well versed in the way the JVM uses memory, I've read up on it and played with settings and such, but it's an overwhelming topic - especially for those of us with no Java development experience. It seems that when calling the method over inline code, sometimes the ColdFusion service just collapses and the request never ends. Other times it does indeed complete, although way too slowly. This suggests that the request can complete only when the server has the resources to handle it - and thus that the method call itself is consuming memory... (?)</p>
  
  <p>If indeed the calling of a method has an overhead attached, I have a big problem. It's not really feasible to move all of this code inline, (while the function in question is simple, there are plenty of other functions that I will need to make use of) and doing so goes against everything I believe as a developer!!</p>
  
  <p>So, any help would be appreciated.</p>
  
  <p>Just for clarity and because I'm sure someone will ask for it, here's the code in question:</p>
  
  <p><strong>EDIT:</strong> As suggested, I've changed the code to use a struct lookup rather than CFSwitch - below is amended code for reference, however there's also a test app in pastebin links at the bottom.</p>
  
  <p><strong>Inside the init method:</strong></p>
  
  <pre><code>    &lt;cfset  Variables.VehicleCategories = {
              'T1'    : 'Beetle'
          ,   'T1C'   : 'Beetle Cabrio'
          ,   'T2'    : 'Type 2 Split'
          ,   'T2B'   : 'Type 2 Bay'
          ,   'T25'   : 'Type 25'
          ,   'Ghia'  : 'Karmann Ghia'
          ,   'T3'    : 'Type 3'
          ,   'G1'    : 'MK1 Golf'
          ,   'G1C'   : 'MK1 Golf Cabriolet'
          ,   'CADDY' : 'MK1 Caddy'
          ,   'G2'    : 'MK2 Golf'
          ,   'SC1'   : 'MK1/2 Scirocco'
          ,   'T4'    : 'T4'
          ,   'CO'    : 'Corrado'
          ,   'MISC'  : 'MISC'
      } /&gt;
  </code></pre>
  
  <p><strong>Function being called:</strong></p>
  
  <pre><code>&lt;cffunction name="getCategory" returntype="string" output="false"&gt;
      &lt;cfargument name="vehicleID" required="true" type="string" hint="Vehicle type" /&gt;
  
      &lt;cfscript&gt;
          if (structKeyExists(Variables.VehicleCategories, Arguments.VehicleID)) {
              return Variables.VehicleCategories[Arguments.VehicleID];
          }
          else {
              return 'Base SKUs';
          }
      &lt;/cfscript&gt;
  &lt;/cffunction&gt;
  </code></pre>
  
  <p>As requested, I've created a test application to replicate this issue:</p>
  
  <p><a href="http://pastebin.com/KE2kUwEf" rel="noreferrer">http://pastebin.com/KE2kUwEf</a> - Application.cfc</p>
  
  <p><a href="http://pastebin.com/X8ZjL7D7" rel="noreferrer">http://pastebin.com/X8ZjL7D7</a> - TestCom.cfc (Place in 'com' folder outside webroot)</p>
  
  <p><a href="http://pastebin.com/n8hBLrfd" rel="noreferrer">http://pastebin.com/n8hBLrfd</a> - index.cfm</p>