coldfusionlucee

Object with static methods vs object methods -> which is more effecient?


In Lucee / Coldfusion, if I have an object that doesn't do anything in an init() nor has properties, and all that it has are helper functions, which of the following two would be more efficient performance / memory wise, and why please?

// public object based function
var rsData = new foo.bar.Object().getList();

vs

// public static function
var rsData = foo.bar.Object::getList();

I've been debating this and am not sure of the answer.


Solution

  • This is a "begging the question" situation.

    Performance ought not be a consideration here, as the difference will be negligible. If you haven't even written the first iteration of the code yet, and don't yet have a performance-bound situation to address: this is a case of premature optimisation.

    If the CFC is a library rather than the representation of an object: use class (static) methods. If it's a representation of an object: use object methods. That's what you ought to be reasoning. Not some (entirely perceived, at this point) performance consideration.

    Bear in mind that for testing purposes, inline method calls - which static methods lend themselves more to - are not possible to bypass (eg: via mocking / stubbing / doubling), so one ought to be... circumspect... about the static method's usage. If it's purely algorithmic so performant and has no side effects, then this is not an issue. This is often not the case as poorly planned/organic code evolves. Also bear in mind that a static reference (MyClass::myStaticMethod) is a hard-coded dependency, and innately increases the complexity of the code it is within.

    These are the sorts of considerations one ought to be making before using static method calls. Not performance.

    Intrinsically, that said, static method invocation requires the class to be loaded once for the lifetime of the app, whereas an object method has overhead of the object being created. That kinda goes without saying though.