Performance tuning for ColdFusion applications and Comment
Kunal Saini, an Adobe employee, recently posted an article on Adobe Developer Connection about Performance tuning for ColdFusion applications. This is a well written article full of useful tips and practices and should be a must read on the topic.
I will raise a counter point to one of the minor tips Kunal raised. He says:
compare() and compareNoCase()
Use compare() or compareNoCase() instead of the is not operator to compare two items. They are a bit faster.
I trust Kunal has insider knowledge about the implementations of these two compare functions, because I fail to see how a straight evaluation (<cfif dan IS 1337>) can be slower than a function call ( compare(dan, 1337) IS 0 ). Maybe it has to do with the type inference and type conversion ColdFusion does as a dynamically typed language, maybe it is something else. Regardless I avoid using compare() and compareNoCase() because both functions reduce the readability of the code.
Whereas all boolean comparisons in ColdFusion treat 1 as true and 0 as false, the compare and compareNoCase functions return 0 if the comparison is true. This means compare( 1, 1) will return 0, which doesn't follow the boolean rules. Since this does not follow the rules, code using compare and compareNoCase is harder to read, harder to follow, and generally uglier than straight comparisons.
So Kunal, I don't take anything away from your statements and I appreciate you writing the article. I want to point out that software isn't all about micro-performance, it is also about long-term maintainability. Always write your code to be readable by others.
Of course, if you happen to write the next Facebook and you need to squeeze every possible fraction of a millisecond out of a routine, then throw this advice right out the window. But then again, you'd have already tuned every single query permutation, added a clustered caching layer, offloaded your static files to a Content Delivery Network and clustered your infrastructure Horizontally and Vertically, haven't you?
There are no comments for this entry.Add Comment Subscribe to Comments
Having worked for Allaire-Macromedia as a consultant, largely tuning CF applications, I often wonder if the suppliers of ColdFusion, currently Adobe of course, should not have some sort of similar consulting division today.
I think the thing to remember about compare() and its friends is that they are not designed to be Boolean functions; they're gt/eq/lt functions, and as a result, they don't map well to Boolean logic. (At least not in a world where 0 is false! Not that I can recall a scenario where 0 was true, not in code, at least. Maybe in a specific app.)
Having said that, I definitely agree that they affect readability. I would prefer to use the comparison operators myself ...
I agree 100%. I would highly favor readability over performance in this case since compare() and compareNoCase() are extremely counter-intuitive.