Skip to content
Advertisement

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?

Advertisement

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement