Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize JS code

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to optimize JS code". In daily operation, I believe many people have doubts about how to optimize JS code. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to optimize JS code". Next, please follow the editor to study!

Suppose you see code like this:

If (a & & d | | b & c & &! d | | (! a | |! B) & & c) {console.log ('pass')} else {console.log (' fail')}

Do you spray old blood on the screen?

Of course, this code was forged by referring to a Nuggets boiling point (@ Li Ergou in the next village). I hope you and your colleagues don't write it that way.

Can write this and or not, if it is not illogical, then it is estimated that there is only one possibility, the requirements change many times, the result of many people modification. no, no, no.

The question now is, in the face of such code, such complex logic, can it be optimized?

The answer is yes!

The exploration of this problem comes from a subject of mathematics: Boolean algebra or logical algebra.

Speaking of Boolean, we developers are all too familiar with this. It's just true and false.

Boolean algebra should be taught more or less in college. Because I majored in mathematics, I must record this kind of problem and review it by the way.

To facilitate the expression of and or no in JS, I use the representation in Boolean algebra:

So a & & d | | b & c & &! d | | (! a | |! B) & & c can be expressed as:

The priority is much clearer after the conversion.

The question now is how do we simplify this logical expression.

Fortunately, there are some common conclusions for us to use:

The first four formulas are easy to understand. For example, oneself or oneself is not, of course, it is true.

The key is that the fifth formula is not so intuitive and can be explained simply by drawing (other formulas can be similarly deduced).

First of all, look at the part, such as the green area in the picture:

Look again, such as the green area in the picture:

The sum of the two is:

And part of it is the pink area in the picture:

Because the pink area is already in the sum of the first two, it doesn't matter how many times you add it.

Note the core feature of the last formula: harmony occurs with and.

With these formulas as the groundwork, we can formally deduce (in the original boiling point matching diagram):

In accordance with article 4, replace the last item:

Note that the first two items are and, respectively, in accordance with Article 5:

The last two items at this time are in accordance with Article 2:

The last two items, one of which is, and the other also contains, satisfies Article 3:

So far, the simplification is over. I really can't think about it. B is gone!

At this point, the opening code is simplified like this:

If (a & & d | | c) {console.log ('pass')} else {console.log (' fail')} this is the end of the study on "how to optimize JS code", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report