JS. If the condition is only with 1 other, does it check the execution order?

I was working on a project, and this problem appeared in my head. I’m not sure that there is already such a record, but I did not find it.

Let's 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 in any case, the if-statement will have to check once from the conditions, if the frequency of actions is known (for example, I know that the else part is executed most often). Does "non-conditions" check that the code is faster or does "not-state" checking not even affect performance? (Suppose I create a complex node.js server that has many of these types of functions.)

Add-on Question: Do most programming languages ​​know theoretically?

I'm just wondering if this affects performance (practically or theoretically).

+5
source share
1 answer

Short answer: No, this does not affect the calculation time.

Long answer: Most Javascript engines (Spider Monkey and V8) use a Just in Time compiler that will iron out these differences at runtime.

In low-level programming languages ​​(read: C / C ++ and ilk) you can get something if you can prevent branch mis-forecasts , as in C ++ code , but the gain is usually only worth it if you are doing kernel programming or doing extreme microoptimization.


Theoretically, whether it matters or not depends on which compiler you are looking at. If you look at a compiler that can learn from traces of code, for example. Just in Time compiler, then it will matter if a certain pocket of code is a hot spot or not. If you look at static compilers, this may mean that the compiler can use one less clock cycle in one of the cases (either if or else ), preventing jmp , but it will depend on the compiler implementation.
+4
source

All Articles