Loops are the most common structure in programming, and optimizing loops is an important part of performance optimization.
- Impairment iterations: Most loops use an iterator that starts at 0 and increases to a specific value. In many cases, iterators with constant impairment in the loop are more efficient starting with the maximum value.
- Simplified termination conditions: As previously stated, property lookups or other O (n) operations should not appear in the termination condition.
- Simplify the loop body: The loop body is the most executed, make sure there are no dense calculations that can be easily removed from the loop.
- Post-Test loop: The most commonly used for and while loops are pre-test loops, and as Do-while is a post-test loop, you can avoid the calculation of the initial termination condition and therefore run faster.
When the number of cycles is determined, no loops tend to be faster. For example, an array has three elements, a direct array operation, and an expansion loop eliminates the additional overhead of establishing loops and processing termination conditions. If the iterations in the loop cannot be determined beforehand, a technique called the Duff device can be used. This technique was named after its creator, Tom Duff, and was first used in the C language. Jeff Greenberg implements the Duff device with JavaScript. The basic concept is to expand a loop into a series of statements by calculating whether the number of iterations is a multiple of 8.
//Credit:jeff Greengerg for JS implementation of Duff ' s Devicevariterations = Math.floor (values.length/8 );varstartAt = values.length% 8 ;vari = 0; Do { Switch(startAt) { Case0:process (values[i++]); Case7:process (values[i++]); Case6:process (values[i++]); Case5:process (values[i++]); Case4:process (values[i++]); Case3:process (values[i++]); Case2:process (values[i++]); Case1:process (values[i++]); } startAt= 0;} while(--iterations > 0);
The Duff device is implemented by dividing the number of elements in the values array by 8来 to calculate how many iterations the loop requires, and then using the lower function of rounding to ensure that the result is an integer. Of course, there may be some elements that cannot be processed, this number is stored in the Startat variable, and the first execution will make an extra call to it.
The faster Duff device is presented by Andrew B. King at speed up Your Site (New riders,2003):
//credit:speed up Your Site (New riders,2003)variterations = Math.floor (VALUES.LENGTH/8);varleftover = value.length% 8;varI=0;if(Leftover >0){ Do{process (values[i++]); } while(--leftover >0);} Do{process (values[i++]); Process (Values[i++]); Process (Values[i++]); Process (Values[i++]); Process (Values[i++]); Process (Values[i++]); Process (Values[i++]); Process (Values[i++]);} while(--iterations > 0);
This method is almost 40% faster than the original Duff device.
Using expansion loops for large values can save a lot of time, small amounts of data, and extra overhead.
JavaScript Loop---performance optimization