High-performance String Concatenation in JavaScript
Do you wanna test it
The first uses the string concatenation operator:
- // standard string appendvar str = "";for (var i = 30000; i > 0; i--) { str += "String concatenation. ";}
The second uses an array join:
- // array joinvar str = "", sArr = [];for (var i = 30000; i > 0; i--) { sArr[i] = "String concatenation. ";}str = sArr.join("");
The truth is rather more complex.
: standard string appends are usually quicker than array joins, but both complete in less than 10ms. : again, standard appends are quicker, but the difference is marginal—often 15ms compared to 17ms for an array join. : the browser normally takes around 30ms for either method. Array joins usually have the edge, but only by a few milliseconds. : a standard append requires 30ms, whereas an array join is more than double—typically 70ms. : bizarrely, a standard append takes no more than 5ms but an array join is more than ten times slower at 55ms.: If you're supporting IE7, array joins remain the best method for concatenating a large number of strings. 就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: