Home > Article > Web Front-end > Discussing the performance of string concatenation under Javascript_javascript skills
1 How to perform string concatenation?
First let us review the two common methods of string concatenation:
1.1 Use the string concatenation operator
Commonly used languages ( Such as Java, C#, PHP, etc.) all have string concatenation operators, and Javascript is no exception. Code example:
浏览器 | copyByOperator | copyByArray |
---|---|---|
IE 6 | 1780.4ms | 9.5ms |
IE 7 | 1754.6ms | 7.7ms |
IE 8 | 1.6ms | 9.4ms |
The test results of IE6, 7 and IE8 are far apart. It is obvious that IE 8 has optimized the string concatenation operation, and the efficiency is already higher than the array copy method .
The following uses a relatively large times value (50000) to test the latest versions of various browsers.
浏览器 | copyByOperator | copyByArray |
---|---|---|
IE 8 | 21.8ms | 54.7ms |
Firefox 3.6 | 40.9ms | 27.9ms |
Chrome 4 | 4.4ms | 10.9ms |
Safari 4.0.5 | 41.6ms | 34.6ms |
Opera 10.50 | 33.1ms | 23ms |
This result is really unexpected. The string concatenation operation under IE 8 actually defeats all browsers except Chrome. Those who always say that IE performance is low should pay attention. In Chrome, string concatenation operations are more efficient than array copying methods.
The performance of browsers is constantly improving. As a front-end engineer, you must also adjust the optimization strategy of Javascript programs in a timely manner to obtain the best performance.