In normal open, when it comes to logical judgment, we generally use if in most cases, and switch occasionally. However, when there are more conditions (more than 5), switch can be used as much as possible. It turns out that in addition to code readability, switch is faster than if else. Compared with the if else, the realization of the switch to the branch table index to optimize (understanding can look here: en.wikipedia.org/wiki/Switch… , and switch statements are compared using the congruent operator, which does not incur type conversion wastage. Example:

if
const start = new Date().getTime() const a = 6 for(let i = 0; i < 9999999; i++) { if (a === 1) { } else if (a === 2) { } else if (a === 3) { } else if (a === 4) { } else if (a === 5) { } else { } } const end = new Date().gettime () console.log(end-startCopy the code
switch
const start = new Date().getTime() const a = 6 for (let i = 0; i < 9999999; i++) { swich(a) { case 1: ; break; case 2: ; break; case 3: ; break; case 4: ; break; case 5: ; break; default : ; }} const end = new Date().gettime () console.log(end-start) // run in 1000 secondsCopy the code

What happens if we just put the condition in the first place

const a = 1; // change this to 1 for(let I = 0; i < 9999999; I++) {if (a = = 1) {/ / went to satisfy the conditions here} else if (a = = 2) {} else if (a = = 3) {} else if (a = = 4) {} else if (a = = 5) {} Else {}} // Now the execution time is infinitely close to the switch execution time, around 1100 // If a = 2 then the execution condition is around 2200 milliseconds, so in the if-else statement, Its conditional statements should be arranged from the highest probability to the lowest probability of occurrence. 支那Copy the code
When we use if, we should reduce the number of judgments and use nested statements

If you assume that the probability of a is the same, then you can reduce the if-else, and you can break it up into pieces.

const start = new Date().getTime(); const a = 6; for(let i = 0; i < 9999999; i++) { if (a <= 3) { if (a == 1) { } else if (a == 2) { } else { } } else { if (a == 4) { } else if (a == 5) { } else { }}} const end = new Date().gettime () console.log(end-start) // Now its execution time fluctuates around 3200 msCopy the code

Obviously, this way of writing, you can imagine the readability. In both if and switch can be used and judge more than 5 conditions, switch writing and performance is better.

So is there another way to write it?

In fact, the best way to optimize conditional statements in a project is to avoid if-else and switch statements and instead query through arrays and objects, called lookup Tables.

An example from my actual development: jump to a different page by judging conditions
function getUrl(type) { if(type = 'a') { return '/page/index/a' } else if(type = 'b') { return '/page/index/b' } else if(type = 'c') { return '/page/index/c' } else if(type = 'd') { return '/page/index/d' } else { return '/page/index/e' }  } const url = getUrl(tupe) wx.nevigateTo({ url : url })Copy the code
Object to write
const obj = {
    a : '/page/index/a',
    b : '/page/index/b',
    c : '/page/index/c',
    d : '/page/index/d',
    default: '/page/index/e'
}

const url = obj[type] || action['default']

wx.nevigateTo({
    url : url
})

Copy the code

The subtlety of this approach is that it takes the judgment condition as the attribute name of the object and the processing logic as the attribute value of the object. When clicking buttons, this method is especially suitable for the case of single condition judgment, that is, logical judgment by the way of object attribute lookup.

So what else is there? The answer is yes!
const maps = new Map([
    [a : '/page/index/a'],
    [b : '/page/index/b'],
    [c : '/page/index/c'],
    [d : '/page/index/d'],
    [default: '/page/index/e'],
])

const url = maps.get(type)

wx.nevigateTo({
    url : url
})
Copy the code

** Using a Map instead of an Object has many advantages. There are some differences between a Map Object and a normal Object:

  • An object usually has its own prototype, so an object always has a “Prototype” key
  • The key of an object can only be a string or symbol, but the key of a Map can be any value
  • You can easily get the number of key-value pairs in a Map by using the size attribute, whereas the number of key-value pairs in an object cannot be obtained directly

Conclusion:

1. Switch performs better than if-else

2. Try to avoid using switches and if else in the project and use lookup tables instead

3. New Map is written in ES6 and has obvious advantages over traditional objects

In addition, WeakMap structure is similar to Map structure and is also used to generate a set of key-value pairs.

A quick note by the way:

  1. WeakMap only accepts objects as key names (except null) and does not accept values of other types as key names.

  2. The object to which the key name of WeakMap points is not included in the garbage collection mechanism.

For details, see ES6 Set,Mep