I have been working with laravel (PHP) recently, and there is an option when using their fluent ORM to define where clauses using closures. This gives us two, functionally equivalent, ways to specify a where clause that should only be added in certain cases:
Example 1
$query = App::make('Vendor_Orders')->
where(function($query) use ($admin) {
if (!$admin) {
$query->where('vendor_id', '=', $this->id);
}
})->
get();
Example 2
$query = App::make('Vendor_Orders');
if (!$admin) {
$query->where('vendor_id', '=', $this->id);
}
$query = $query->get();
As you can see, one of these is using a closure (and maintaining the "fluent-ness"), while the other avoids it. My instinct is to prefer the second, because using an anonymous function like that is adding an extra stack frame, which I would expect to make the program less efficient (possibly a holdover from my time working with C). On the other hand, my colleague (whose first language was PHP) prefers the first example, because it avoids the use of a second assignment operator, and he doesn't see anything wrong with piling up extra function calls.
This started me wondering - how much effect does that extra stack frame really have, in a modern system? Does it make any practical difference if one uses a function call instead of an if statement?
I'm not looking for subjective which-do-you-prefer answers, I'm asking for some sort of facts regarding the impact of unnecessary jumps - particularly to anonymous functions - in modern, interpreted languages (e.g. PHP, Java, etc.). Possibly also some notes on how this differs from old-school compiled languages.