'Which is faster: CFM page full of CFQUERY calls or CFINVOKE calls?
Which would be faster and more efficient memory-wise?
- A CFM file containing individual CFQUERY calls
- 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.
Solution 1:[1]
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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Max Voisard |
Solution 2 | Adam Cameron |