I was working on a project and this problem popped into my head. I am not sure if there is already a post like this but I have found none.
Let say if I have this:
function checks(s) {
if (s.length == 4 && s[0] == "a") {
//action
} else {
//other action
}
}
checks("a001");
checks("else");
checks("else");
checks("else");
and this:
function checkb(b) {
if (b) {
//action
} else {
//other action
}
}
checkb(true); //Suppose i'm passing these through another function
checkb(false); //so it makes sense for me doing these
checkb(false);
checkb(false);
Since either way the if-statement is going to have to check once of the conditions, if the frequency of the actions are known (e.g. I know that the else part is performed most often). Does checking for "not conditions" make the code run faster or does the checking of "not condition" don't even affect the performance? (Suppose I'm building a complicated node.js server that has many of these type of functions.)
Addition question: Do most programming languages do the same too theoretically?
I was just wondering if it affects the performance (practically or theoretically).