Which is faster? if () return; else return; OR if () return; return;

During coding, I just asked myself this question:

It's faster:

if(false) return true;
 else return false;

What is this?

if(false) return true;
return false;

Of course, if there is a difference, this is ridiculous, but my curiosity will not go away until I find out that: D

+5
source share
1 answer

Simply:

return !false;

So in a real example

return !$this->isSth();

// Not

if ($this->isSth) {
    return false;
} else {
    return true;
}

Performance is not important here - each decision is very fast, there is no need for optimization. Remember the words of Donald Knuth:

Premature optimization is the root of all evil

+15
source

All Articles