Fourth Chapter
Algorithm and Process Control
The main factors that affect code performance are the organization of code and the idea of solving specific problems
Cyclic processing is one of the most common programming patterns and one of the focus points for improving performance
Four types of loops: standard for loop
for (var0; i < things.length; i++) { things[i]//Looping theme }
While loop
var i =0; while (i<) { / /Prerequisites // cycle theme i++; }
Do-while Cycle:
var i =0; Do { // Loop theme }while (i++ <// Post-Test conditions
For-in Cycle
for (varinobject) { // Loop theme }
The pro variable assigns a property name to object that returns properties that include the object instance property and the inherited property in the prototype chain
Only for-in loops in the four cycle types are significantly slower than the other four
Do not use for-in to iterate through the members of an array
The selection of the loop type should be based on demand rather than performance
Reduce the effort of the iteration:
The first step in loop optimization: Reduce the number of lookup times for object members and arrays
Improving the performance of this loop is simple, looking for a property only once, storing the value in a local variable, and then using that variable in the control condition;
var len = Len=things.length //storage variable len
For (var i = 0; I < Len; I+ +) {
//Cycle theme
}
You can also reverse the array to improve loop performance
Reduces the property lookup and reverses the for (var i =things.length; i--;) { things[i] }
The control condition is compared with true, and the value that is not 0 to True equals 0 is false;
Reduce the number of iterations
Can achieve more significant new performance improvements, the most well-known model is: Duff equipment;
Basic concept: A maximum of 8 process () loop iterations per loop can be called the total number divided by 8; Since not all numbers can be divisible by 8, variable stratat is used to hold the remainder; indicates how many times the first loop should be called, and if it is 12, the first loop is called 4 times.
The second call 8 times replaces 12 cycles with two cycles;
<script type= "Text/javascript" >vari = Items.length%8; while(i) {process (Items[i--]); } I= Math.floor (ITEMS.LENGTH/8); while(i) {process (Items[i--]); Process (Items[i--]); Process (Items[i--]); Process (Items[i--]); Process (Items[i--]); Process (Items[i--]); Process (Items[i--]); } </script>
For example, in 500000 this iteration will run 70% less than the regular cycle;
If-else and switch
The most popular method is based on the number of test conditions, the larger the more inclined to switch instead of If-else
Dichotomy divides the range into a series of intervals, and then gradually reduces the scope, the average time for the code to run is about half the previous one, this method is very suitable for multiple range of needs testing time (if the discrete value of the switch more appropriate)
High-performance JavaScript reading notes-4