Algorithm and Process Control

Source: Internet
Author: User
1. Loop

For most programming languages, code execution time is mostly consumed in a loop to process a series of things cyclically, which is one of the common modes. Therefore, it is also one of the most important aspects to improve performance, understanding the impact of javascript loops on performance is crucial. an endless loop or a long-term running loop can cause serious user experience.
Loop typeThe third edition of ECMA defines the basic syntax and behavior of javascript. There are four types of loops. The first type is that the for loop has the same syntax as the C language: for (var I = 0; I <10; I ++) {// do some thing} for Loop is a common loop structure. It consists of four parts: initialization, front-side conditions, post-execution body, and loop body. When the code runs to the for loop, the initialization code is run first and then the front-side conditions are entered. If this parameter is set to true, the system runs the loop body. After the loop body is executed, the Execution Code starts to run. The clear and clear code style of the for Loop is favored by developers. Note: When a var statement is initialized in a for loop, a function-level variable is created instead of a loop-level variable. Since javascript only has function-level scopes, defining a new variable in a for loop is equivalent to defining a variable in a loop in vitro. The second type of loop is a while loop. If a precondition is true, the loop starts. Otherwise, the loop exits. The third type of loop is the do-while loop, which is the only loop with a post condition, so the code runs at least once. The fourth type of loop is the for-in loop, which is special. It can be used to enumerate the attribute names of objects. Of course, the attribute name must be enumerable and can be returned only when the variable is complete. Therefore, the returned attributes include the object case attributes and attributes inherited from the prototype chain. Cyclic PerformanceTo say loop performance, for-in has the worst performance because he needs to search for examples or properties in the prototype for each operation, so he will produce more performance overhead, so it is slower than other loop types. Compared to loops with the same number of iterations, for-in is only about 1/7 of the speed of other types. Unless you traverse an object with an unknown number of attributes, do not use the for-in loop, if you traverse a list of known attributes, it will be faster to use other types of loops. Note: Do not use for-in to traverse all types of loops except for-in. As for the Type selected, it depends on the requirement rather than the performance. If the loop type has nothing to do with performance, how should we choose? 1. Transactions processed in each iteration 2. By reducing one of the two iterations, you can have a positive impact on the overall performance of the loop. Reduce iteration workload

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.