Skip to content

What’s the performance difference between “skip if condition” and “directly return”?

Is there any performance difference between the following 2 functions:

function a() {
    var a = false;
    if(a == true) {
           ... Many lines, e.g. 1 million lines ...
    }
}

function b() {
    var a = false;
    if (a != true) {
        return;
    }
           ... Many lines, e.g. 1 million lines ...
 }

Which one has a smaller execution time?

Answer

I don’t think there is a performance difference, but the second function is better for readability, because you don’t have to indent. Also you can use !a in the if statement in the second function for better readability.