Which is faster: CFM page full of CFQUERY calls or CFINVOKE calls?

182 views Asked by At

Which would be faster and more efficient memory-wise?

  1. A CFM file containing individual CFQUERY calls
  2. A CFM file containing individual CFINVOKE calls to CFC's containing individual methods for each of those same queries

I realize there are different ways of invoking methods of a component, such as using createobject. For now, let's limit the discussion to just the above two options.

I don't have any sample code. Just want to know what would be considered best practice in the above situation.

Also, I am using newer versions of Adobe ColdFusion (2016, 2018, 2021), so Lucee is not an option.

2

There are 2 answers

0
Max Voisard On BEST ANSWER

In short, option #2 is the recommended practice. You want to store back-end processing in CFC files, which act like objects of entity classes and controller classes, all as part of the CFM file, which acts as a presentation class. These protocols all stem from the object-oriented programming paradigm.

Now, with using component methods generally being the better choice for holding queries, (1) it doesn't mean you are constrained to this option and (2) that doesn't answer the question of which performs better. Yet, the query should still probably be called from a component method. Since you have the same query, you can simply make one stateful query object and the data and the methods can be encapsulated in the same memory space, leading to an overall performance boost.

3
Adam Cameron On

I think the question is naive. Option (1) will be faster because it involves less code and fewer moving parts. I would really like to see the test mentioned in the comments that claims the "cfinvoke" version is faster. I suspect the test is flawed, or not testing like-for-like.

However this sort of performance consideration is the sort of thing one looks at after one has written good code (so... not the first option. Definitely not that), and one detects a performance issue. Real world performance gains will seldom be made in this sort of code differentiation.

Write good, clean, well-designed, easy-to-maintain code. Do that first. Only revert back to shonky code to try to eke out that last millisecond if you really need to. But still: <cfquery> tags directly in .cfm files is "never" going to be part of that solution.