Reference high-performance JavaScript
The For in Loop uses it to iterate over the object's property name, but each time the operation searches for an instance or a prototype's properties, causing more overhead to traverse with for
The book mentions do not use for in to iterate through an array
1 first for in will find properties on the prototype chain
var arr = [= = "Test"; for (var in arr) {console.log (i); Console.log (typeof i);} In this example, it is found that the field of the attribute is a string and a also acts as the output of the result (which can be filtered through the hasOwnProperty () property of the prototype)
var arr = [1,2,3 = "Test" ; for (var i = 0,len = Arr.length;i < Len;i+=1) { Console.log (i); Console.log ( typeof i);} In this example, each i is number and a does not output as a result
var a = [];a[5] = 5; for (var in a) { console.log (i); This only outputs 5, that is, the for in loop will skip the empty array item} for(var i =0,len = a.length;i < len;i+=1) { Console.log (i); For loop normal output 0 1 2 3 4 5}
How to improve the overall performance of the cycle
(1) Reduce or optimize the constant value of the transaction cache loop for each iteration (not necessarily the length of the array is not constantly looking for a fixed value in the loop) if the order of the array items is not related to the task being performed, you can use a backward-forward approach to this scenario. This reduces the number of iterations compared to the operation, but also optimizes the performance
(2) Reduce the number of iterations
Multiple iterations in a single iteration can reduce the number of iterations of the loop as a whole
variterations = Math.floor (ITEMS.LENGTH/8), startAt = items.length%8, I= 0; Console.time (A); Do { Switch(startAt) { Case0:process (items[i++]); Case7:process (items[i++]); Case6:process (items[i++]); Case5:process (items[i++]); Case4:process (items[i++]); Case3:process (items[i++]); Case2:process (items[i++]); Case1:process (items[i++]); } startAt= 0;} while(--iterations);
In cases where this mode is not high enough (e.g. 10000) I find that the speed of this mode is not as fast as the traditional loop processing, but rather slower when the order of magnitude (100000) does improve performance
Refine conditional statements
Select the appropriate conditional statement according to the situation
(1) In general, if else is more suitable for two discrete values or several different value domains when the condition is more than two discrete values, the switch statement is a more choice
(2) Optimize if else to make sure that the most likely branch is in the first branch of the If else, the idea that you can get to the condition as soon as possible
Using Memoization
The idea is to cache the results of a previous calculation for use in the next calculation, the techniques commonly used in recursive algorithms
functionfactorial (n) {if(n = = 0) { return1; } Else { returnN*factorial (n-1); }}console.time (A); factorial (16); factorial (11); factorial (10); Memfactorial (10); Memfactorial (10); Memfactorial (10); Memfactorial (10); Console.timeend (A);functionmemfactorial (n) {if(!memfactorial.cache) {Memfactorial.cache= { "0": 1, "1": 1 }; } if(!memfactorial.cache.hasOwnProperty (n)) {Memfactorial.cache[n]= N*memfactorial (n-1); } returnmemfactorial.cache[n];} Console.time ("B"); Memfactorial (16); Memfactorial (11); Memfactorial (10); Memfactorial (10); Memfactorial (10); Memfactorial (10); Console.timeend ("B");
Since we have cached the results of the previous calculations, we will greatly improve performance when we need this result many times later (I find that performance does not improve when the results of previous calculations are not repeated, and the performance of the JS engine is improved and the property lookup also requires some performance)
High-performance JavaScript Learning Note Series (4)-Algorithmic and process Control