Skip to main content
Inspiring
February 21, 2012
Answered

The better way to reference a component

  • February 21, 2012
  • 1 reply
  • 2019 views

In general, when something can be used by all visitors in 1 application, I store that into the application scope for quick memory-based performance.

My application has a component called "dao.cfc" (handles data access) and quite literally every user on every request is going to use that component at least once per load.

Is there any performance boost in storing a copy of the dao.cfc into the application scope vs. just pathing out to it as it is needed, ie:

IN APPLICATION SCOPE (the dao component is stored in the application.com structure)

#application.com.dao.functionName( args, args, etc. )#

vs.

AS NEEDED PER REQUEST

<cfinvoke component="app.com.dao" method="functionName" arg="value" etc="etc">

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    Yes the CFINVOKE call has to create a new object every time (even if it's discarded automatically once you're done), whereas with the application-scoped approach, you're just creating the one object.

    Creating objects has a fairly high overhead compared to most other in-memory operations in CF.

    If you have a stateless object - which a DAO ought to be - then there's no reason to keep creating new instances of it every time you need to call one of its methods.

    --

    Adam

    1 reply

    Inspiring
    February 21, 2012

    Well yeah: obviously creating a transient object any time you need one is going to have overhead that reusing the same object is not going to have.

    --

    Adam

    Inspiring
    February 21, 2012

    But I'm using them both the exact amount of times.  Are you saying that:

    #application.com.dao.functionName( args, args, etc. )#

    #application.com.dao.functionName( args, args, etc. )#

    #application.com.dao.functionName( args, args, etc. )#

    will always be faster than

    <cfinvoke component="app.com.dao" method="functionName" arg="value" etc="etc">

    <cfinvoke component="app.com.dao" method="functionName" arg="value" etc="etc">

    <cfinvoke component="app.com.dao" method="functionName" arg="value" etc="etc">

    So the amount of effort it takes for CF to store the dao into the application scope and then call it for all users is less than each user invoking a component as needed.

    Adam Cameron.Correct answer
    Inspiring
    February 21, 2012

    Yes the CFINVOKE call has to create a new object every time (even if it's discarded automatically once you're done), whereas with the application-scoped approach, you're just creating the one object.

    Creating objects has a fairly high overhead compared to most other in-memory operations in CF.

    If you have a stateless object - which a DAO ought to be - then there's no reason to keep creating new instances of it every time you need to call one of its methods.

    --

    Adam