Concatenate multiple strings. There are usually three methods that are frequently used in practice. Concatenate multiple strings. There are usually three methods that are frequently used in practice.
Use the string connector '+', 'string1' + 'string2' +...
Use the join function of the array. First, write the string into the temporary array, and then call the join method of the array to connect the string elements.
Use the concat function of the string.
Method 1: Use the string connector '+'
var concat1 = function(str1, str2){ return str1 + str2;};
Method 2: Use the join function of the array
var concat2 = function(str1, str2){ var arr = []; arr.push(str1); arr.push(str2); return arr.join();};
Method 3: Use the concat function of the string
var concat3 = function(str1, str2){ return str1.concat(str2);};
Performance summary
I used Benchmark locally to compare the performance of the above two methods. The test environment is Testing in Chrome 46.0.2490/Mac OS X 10.10.4. The results are as follows:
concat#+ x 90,483,047 ops/sec ±2.06% (84 runs sampled)concat#array-jion x 12,303,912 ops/sec ±0.90% (82 runs sampled)concat#string-concat x 40,845,196 ops/sec ±0.83% (89 runs sampled)Fastest is concat#+
That is to say, in chrome 46, the efficiency of using the string connector '+' is much higher.
Of course, this is only a test in chrome 46, and does not represent all browser platforms.
The jsPerf also has a similar performance test https://jsperf.com/concat-vs -...
The test results are as follows:
Using join in the old browser (ie7-) is more efficient.
In modern browsers, try to use "+" to make it more efficient.
Of course, in a few modern browsers, "+" may not be faster than join (for example, safari 5.0.5, opera 11.10)
It is a string array, and it is better to directly join.
Between "+" and concat, it is preferred to use "+", which is convenient, intuitive, and efficient.