[Js computing performance series] concatenate string methods and Performance Comparison

Source: Internet
Author: User
Tags string methods
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.