日期:2014-05-16  浏览次数:20404 次

High Performance JavaScript 读书笔记(四)

四.Algorithms and Flow Control
??? The overall structure of your code is one of the main determinants as to how fast it will execute. Having a very small amount of code doesn’t necessarily mean that it will run quickly, and having a large amount of code doesn’t necessarily mean that it will run slowly. A lot of the performance impact is directly related to how the code has been organized and how you’re attempting to solve a given problem.
? ?The techniques in this chapter aren’t necessarily unique to JavaScript and are often taught as performance optimizations for other languages. There are some deviations from advice given for other languages, though, as there are many more JavaScript engines to deal with and their quirks need to be considered, but all of the techniques are based on prevailing computer science knowledge.

  • Loops
    ?? In most programming languages, the majority of code execution time is spent within loops. Looping over a series of values is one of the most frequently used patterns in programming and as such is also one of the areas where efforts to improve performance must be focused. Understanding the performance impact of loops in JavaScript is especially important, as infinite or long-running loops severely impact the overall user experience.

    a.Types of Loops
    ?? ECMA-262, 3rd Edition, the specification that defines JavaScript’s basic syntax and behavior, defines four types of loops. The first is the standard for loop, which shares its syntax with other C-like languages:
    for (var i=0; i < 10; i++){
    	//loop body
    }
    
    ??The for loop tends to be the most commonly used JavaScript looping construct. There are four parts to the for loop: initialization, pretest condition, post-execute, and the loop body. When a for loop is encountered, the initialization code is executed first, followed by the pretest condition. If the pretest condition evaluates to true, then the body of the loop is executed. After the body is executed, the post-execute code is run.The perceived encapsulation of the for loop makes it a favorite of developers.
    ??Note that placing a var statement in the initialization part of a for loop creates a function-level variable, not a loop-level one. JavaScript has only function-level scope, and so defining a new variable inside of a for loop is the same as defining a new function outside of the loop.
    ?The second type of loop is the while loop. A while loop is a simple pretest loop comprised of a pretest condition and a loop body:
    var i = 0;
    while(i < 10){
    	//loop body
    	i++;
    }
    
    ??Before the loop body is executed, the pretest condition is evaluated. If the condition evaluates to true, then the loop body is executed; otherwise, the loop body is skipped. Any for loop can also be written as a while loop and vice versa.
    ? ?The third type of loop is the do-while loop. A do-while loop is the only post-test loop available in JavaScript and is made up of two parts, the loop body and the post-test condition:
    var i = 0;
    do {
    	//loop body
    } while (i++ < 10);
    
    ?
    ? ?In a do-while loop, the loop body is always executed at least once, and the post-test condition determines whether the loop should be executed again.
    ??The fourth and last loop is the for-in loop. This loop has a very special purpose: it enumerates the named properties of any object. The basic format is as follows:
    for (var prop in object){
    	//loop body
    }
    
    ?? Each time the loop is executed, the prop variable is filled with the name of another property (a string) that exists on the object until all properties have been returned. The returned properties are both those that exist on the object instance and those inherited through its prototype chain.

    b.Loop Performance
    ?? A con