Does this imply that when I call toArray() outside of a condition I can always go with $modx->resource because the output will be the same as modx->getObject('modResource', $id) but it is faster?
In general, yes. The issue isn't the toArray() specifically, but that toArray() is a method of an object of a class that is already instantiated. It's the fact that you call it inside a
function as an argument in a condition. Every OOP language handles function calls slightly differently, but often have similar implementations when it comes to inline calls. Most inline function calls necessitate a combination of "by reference" and "by value".
Generally, non-inline calls in PHP are all "by value" unless explicitly stated. This means that it copies the object and then runs the function. However (which has cause a lot of criticism for PHP), inline calls work a little differently. If the argument is a method, the object is passed by reference, but the method's return are by value. This is because you are passing a function, not the object, itself.
toArray() is essentially (not quite) a clone for the xPDO object data, which means that as a function, it forces a "by value". But since you pass IT as the argument, because it is a class method, PHP does something funny to it.
When modx->getObject('modResource', $id) is slower than $modx->resource which kind of snippet should be preferred.
Snippet A: Where both templateChunks are stored in variables via $modx->resource before the if condition that determines which templatechunk to show.
Snippet B: Where only the actual templateChunk is returned in the condition via modx->getObject('modResource', $id)?
So my question is basically, two times $modx->resource outside the condition vs one time modx->getObject('modResource', $id) inside the condition?
It depends on how complex your templates are and its surrounding stuff. You have to understand that rarely is the speed dependent entirely on a single function. Often it is repeated operations that make the difference. Looping is typically the largest cause of lack of speed.
If your template requires a lot of snippet calls that perform complex loops, or a ton (and I mean ton) of embedded chunks, Method A would be preferred.
If your page is fast enough and the only thing slowing it down is the actual network transmission, Method B is best.
There are other considerations, of course, but performance is normally the thing most web developers have a problem with, so that was my focus. Honestly, though, optimization like this is the first mistake of great programming. I can't take credit for this, but a programmer I very much agree with once said:
On the topic of Optimization:
Rule #1: Don't do it.
Rule #2 (for experts only): Don't do it. (At least until you already have a viable working solution)
In other words, even though I brought up the point of speed (because that's why $modx->resource is there and accessible as a convenience), don't focus on the semantics. Get a solution.
P.S. Did that sample fix your issue?