One . Reduce the level of judgment
As you can see from the code below , The same reduction in effect judgment levels can optimize performance
Two . Reduce the scope chain query level
A simple explanation , The first execution in the figure below foo Function ,bar Function print in name,bar There is no name, So the scope chain looks up ,foo There is... Inside name,
But it's actually an assignment , And we need to go up and look up , Query in global scope name, Compared to the second one in the figure below , More than one level up , So it's going to take a little longer .
（ Note ： Regardless of memory space consumption , It depends on the product .）
3、 ... and . Reduce the number of data reads
A simple explanation , For multiple frequent calls , Like arrays , Objects are slow , Because according to the reference relationship, we need to find the location in the heap memory first , So reduce the consumption of time ,
We should reduce the number of queries for members , For example, put it in a variable and cache it （ Note ： Regardless of memory space consumption , It depends on the product .）
Four . Literal quantity and construction
A simple explanation , The first one in the picture below new Object() It is equivalent to calling function , It's slower , The second one in the figure below is equivalent to opening up space in the heap area , Put attributes and values in it
Again, for example , The first one in the figure below is just a literal string , The second is equivalent to creating a string object .
5、 ... and . Loop optimization
A simple explanation ： In Figure 2, a frequently used value is cached , Improved execution efficiency .
As shown in the figure below ： Regardless of order ,while The loop decrement ratio for Turn around , More efficient execution
6、 ... and . Reduce the number of declarations and statements
Just explain : For infrequently used data , Caching first is not recommended , Get it when you execute it , To reduce memory consumption during execution, there is a compilation process before execution , The expression
A lot , We should split words into lexical units , Then do a grammar analysis , And then turn it into code and run it , Time is wasted here .
The explanation for reducing the declaration variable is the same as above