Home>

Today when I changed the plugin boxscroll, because the conditions in the if are judged more than two,So immediately thought about rewriting switch. Changed to half,Suddenly remembered a requirement in code quality inspection tools such as jshint,Replace == with ===, without unreliable casts.Then suddenly I guessed,Does switching to switch reduce efficiency?The actual judgment in the switch is == or ===?

With the idea,Hurry up and raise a chestnut,Eat it in one bite:

var a="5";
switch (a) {
  case 5:
    console.log ("==");
    break;
  case "5":
    console.log ("===");
    break;
  default:
}

The last console shows ===, it seems to be safe to use.Looking for the previous study notes,Well, the high school senior did say that switch judges are congruent operators.So no type conversion occurs.To summarize here

1.if and switch

if is the most used,Nothing much to say.It is worth noting that if is actually similar to ||. If conditiona in if (conditiona) {} else {} is true, then it will not see the code in else after executing the code block before else .The same as ||Even with so many mistakes inside.Based on this nature,Of course, put the code block that is probably the most used first.Reduce the number of judgments.on the other hand,If there are many if judgments, and the number of possible executions is evenly distributed,Then the following judgment statement must execute the previous judgment one by one each time.Not conducive to optimization.A better approach is to change one layer of judgment sentences into two layers of judgment sentences.Such as

if (a>0&&a<1) {
  //do something
} else if (a>1&&a&=;2) {
} else if (a>2&&a&=;3) {
} else if (a>3&&a&=;4) {
} else if (a>4&&a<= 5) {
} else if (a>5&&a&=;6) {
} ...

Becomes

if (a>0&&a&=;4) {
  if (a<= 1) {
    //do something
  } else if (a>1&&a&=;2) {
  } else if (a>2&&a&=;3) {
  } else if (a>3&&a&=;4) {
  }
} else if (a>4&&a&=;8) {
  //
} ..

Although each of the previous judgments has been added once more,However, the subsequent judgments have been reduced by (4-1) * n times, and it is still profitable.Suddenly I think this method is a bit like nested loops,Leaving a small number of cycles outside helps performance optimization,How to divide into two or even multiple layers depends on the specific situation.

Switch is the closest comrade of if,Come over every time if is too busy.It is estimated that there is nothing to say between switch and if.And switch is the same as if, they are executed in order from top to bottom,The difference is that the else in if is not used in the switch.It has its own younger brother:break. If no break is encountered, the switch will continue to execute,Such as

var a=2;
switch (a) {
  case 1:
    console.log ("1");
    //break miss
  case 2:
    console.log ("2");
  case 3:
    console.log ("3");
  default:
    console.log ("no break");
}

Finally the console displays 2,3, no break. It ’s actually pretty easy to understand,break is to prompt the program to jump out of the internal execution body to the next case judgment, if not,Equivalent to if (condition) {a} {b}, without else, both a and b must be executed.There are two small tips, one is that you can write any expression in switch and case,Such as

switch (a + b) {
  case a * b:
    console.log ("1");
    break;
  case a/b + c:
    break;
    //...
  default:
    console.log ("no break");
}

The actual comparison is (a + b) === (a * b) and (a + b) === (a/b + c). The second is that switch has a special usage,Such as

switch (true) {
  case condition1:
    //do something
    break;
  case condition2:
    break;
    //...
  default:
    //..
    ;
}

At this time, each case in the switch will be judged and executed in order.As for switch (false), it doesn't do much good.

2. == and ===

One word is done,Apart from not converting the operands before the comparison,The congruence and inequality operators are not different from the equality and inequality operators.

The most classic case

var a="5",  b=5;
a == b //true
a === b //false
var a="abc",  b="ab" + "c";
a === b //true

The reason why the following is true is actually inseparable from the immutability of the string type.It looks like b is just a simple concatenation of a string,But in fact it has nothing to do with the original b.Each string is stored in a specific place in the memory pool.When b="ab" + "c" finishes executing,The strings ab and c have been destroyed,And b points to the location of abc in the memory pool. Because the string abc was found in the memory pool before pointing (because a refers to it,So exists), so b and a point to the same area,The equality judgment is equal.If no variable before b points to the string abc, then there is no in the memory pool,It will draw a place in it for abc, and point b to abc.

Attach two previous summary pictures:

That's all for this article.Hope everyone likes it.

  • Previous PHP implements the method of copying and moving files
  • Next Automatically output web page text with javascript