-1

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?

skyline
  • 443
  • 9
  • 31
  • 1
    the readability of `b` is better. if the paradigm *exit early, exit often* is relevant for you. – Nina Scholz Mar 15 '19 at 08:26
  • I would do `if (a !== true) {` to eliminate possible type conversion bugs. I don't think performance difference here is place to consider. – Justinas Mar 15 '19 at 08:27
  • 1
    I'd say there is no difference in performance. The reason people tends to choose `if (a !== true) return;` over `if (a === true)` is that the former is much easy to read and unserstand, especially when you have "1 million lines of codes". – yqlim Mar 15 '19 at 08:28
  • if(a) checks for all falsy conditions but if (a !== true) checks for only boolean true – AZ_ Mar 15 '19 at 08:30

2 Answers2

1

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.

Cosmin
  • 478
  • 1
  • 5
  • 16
0

The second function should take less time to parse and run than the first, both on page load and when the function is executed.

On page load, the interpreter has to go through all the text, check that its syntax is valid, construct an Abstract Syntax Tree from the result and (on newer engines) compile the result to machine code. Even if most of the a function body is never executed, it still has to be parsed. Parsing 1 million lines of Javascript is a non-trivial task.

When each function is executed, a may take more time to run than b, even if only by a bit - for example, if a defines many variables inside those lines, variables scoped to a:

function a() {
    var a = false;
    if(a) {
      var b = 'b';
      var c = 'c';
      // more lines
    }
}

When the function is run, the interpreter has to look through all the variables defined in the if block which never runs, so that those variable names can be properly initialized. When the function is run, the var b; and var c; and any other vars will be hoisted to the top:

function a() {
  var a;
  var b;
  var c;
  // initialize any other variables local to this function
  a = false;
  if(a) {
    b = 'b';
    c = 'c';
    // more lines
  }
}

If there are many variables to initialize, this may result in the function taking a bit longer than if there were no variables to initialize.

In contrast, a function which has no excess variables to initialize nor many lines to parse:

function b() {
    var a = false;
    if (a != true) {
        return;
    }
 }

will be parsed, compiled, and run more quickly.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320