/*** Background: I found that my colleagues like to use a template (String. replace) to spell JS strings. That's right. Using a template increases readability,
* Compared with the efficient Array. push and then Array. join ("") concatenate strings, replace can be a problem.
* For example, the number of loops is 1000. The gap in ie is large, and the Array. push in ff is nearly doubled. Run the following demo:
* String. replace VS Array. push */var testType = 2; // 1 = String. replace or 2 = Array. push var s = "", sArr = [], n = 1000; // u can change "n" from 1000 to limit for (var I = 0; I <n; I ++) {s + = "{a}";} var t1 = new Date (). getTime (); for (var I = 0; I <n; I ++) {if (testType = 1) s = s. replace ("{a}", "ba"); else sArr. push ("B" + "a");} if (testType! = 1) s = sArr. join (""); var t2 = new Date (). getTime (); alert ("use" + (testType = 1? "String. replace": "Array. push") + ":" + (t2-t1) + "ms ")
The other conclusion is that over 100 ms (which number does it look like ?) Latency can be perceived. If the processing speed of the template scheme is slow (because the template needs to be read first), latency can be perceived and is not allowed in some projects.