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

High Performance JavaScript 读书笔记(五)

五.Strings and Regular Expressions
?? Practically all JavaScript programs are intimately tied to strings. For example, many applications use Ajax to fetch strings from a server, convert those strings into more easily usable JavaScript objects, and then generate strings of HTML from the data. A typical program deals with numerous tasks like these that require you to merge, split, rearrange, search, iterate over, and otherwise handle strings; and as web applications become more complex, progressively more of this processing is done in the browser.
? ?In JavaScript, regular expressions are essential for anything more than trivial string processing. A lot of this chapter is therefore dedicated to helping you understand how regular expression engines internally process your strings and teaching you how to write regular expressions that take advantage of this knowledge. ?Also in this chapter, you’ll learn about the fastest cross-browser methods for concatenating and trimming strings, discover how to increase regex performance by reducing backtracking, and pick up plenty of other tips and tricks for efficiently processing strings and regular expressions.

  • String Concatenation
    ? ?String concatenation can be surprisingly performance intensive. It’s a common task to build a string by continually adding to the end of it in a loop (e.g., when building up an HTML table or an XML document), but this sort of processing is notorious for its poor performance in some browsers.
    ? ?So how can you optimize these kinds of tasks? For starters, there is more than one way to merge strings.
    Method Example
    The + operator str = "a" + "b" + "c";
    The += operator

    str = "a";

    str += "b";

    str += "c";?

    array.join() str = ["a", "b", "c"].join("");
    string.concat()

    tr = "a";?

    str = str.concat("b", "c");

    ? ?All of these methods are fast when concatenating a few strings here and there, so for casual use, you should go with whatever is the most practical. As the length and number of strings that must be merged increases, however, some methods start to show their strength.

    a.Plus (+) and Plus-Equals (+=) Operators
    ? These operators provide the simplest method for concatenating strings and, in fact, all modern browsers except IE7 and earlier optimize them well enough that you don’t really need to look at other options. However, several techniques maximize the efficiency of these operators.
    First, an example. Here’s a common way to assign a concatenated string:
    str += "one" + "two";
    ?When evaluating this code, four steps are taken:

    ?? 1. A temporary string is created in memory.
    ?? 2. The concatenated value "onetwo" is assigned to the temporary string.
    ?? 3. The temporary string is concatenated with the current value of str.
    ?? 4. The result is assigned to str.

    ?? This is actually an approximation of how browsers implement this task, but it’s close. The following code avoids the temporary string (steps 1 and 2 in the list) by directly appending to str using two discrete statements. This ends up running about 10%–40% faster in most browsers:
    str += "one";
    str += "two";
    
    ?? In fact, you can get the same performance improvement using one statement, as follows:
    str = str + "one" + "two"; // equivalent to str = ((str + "one") + "two")
    ??? This avoids the temporary string because the assignment expression starts with str as the base and appends one string to it at a time, with each intermediary concatenation performed from left to right. If the concatenation were performed in a different order (e.g., str = "o