JavaScript Loop---performance optimization

Source: Internet
Author: User

Loops are the most common structure in programming, and optimizing loops is an important part of performance optimization.

    1. 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.
    2. Simplified termination conditions: As previously stated, property lookups or other O (n) operations should not appear in the termination condition.
    3. 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.
    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.