Compare:
if (myVariable) {
doSomething()
}
function doSomething ()
{
// Work goes here
}
vs
doSomething();
function doSomething()
{
if (myVariable) {
// Work goes here
}
}
ie My question is whether it’s faster to do the check outside of the function and avoid a context switch (I think that’s the right term) ) or just do it inside the function because it makes such a minor difference?
Cheers.
It Just Doesn’t Matter (although the first method avoids some work so it should faster, but by an amount which is probably less than statistical noise).
What really matters is which method best represents the logic. Rule of thumb is that every statement in a function should be on about the same level of abstraction. Is the conditional expression more or less abstract than the function call?