JavaScript string concatenation method detailed (performance test)

Source: Internet
Author: User
Tags stringbuffer javascript string concatenation

JS string concatenation has two kinds: with "+" connection, with the join of the array connection.

The code is as follows Copy Code


var a1=10;
var a2=20;
a3 = A1 + a2/Digital plus number
Alert (typeof (A3))//number
Alert ("a1+a2=" +a3)//a1+a2= 30
Alert (typeof ("a1+a2=" +a3))//String "A1+A2" plus number A3; Gets a string, output string
Alert ("a1+a2=" +A1+A2)//a1+a2= 1020
Alert (typeof ("a1+a2=" +a3))//string
Alert ("a1+a2=" +a2-a1)//nan
Alert ("a1+a2=" + (A2-A1))//a1+a2= 10
Alert ("a1+a2=" +number (A1) +number (A2))//a1+a2= 1020
Alert ("A1+a2=" +number (A1+A2))//a1+a2= 1020
Alert (typeof ("A1+a2=" +number (A1+A2))//string

Compare the efficiency of these two methods.

The code is as follows Copy Code

<script>


function Add () {
var s = (new Date ()). valueof ();
var str = ';
for (i = 0; i < 50000; i++)
{
str = i;
}
var e = (new Date ()). valueof ();
alert (e-s);
}
function Add_arr () {
Array = new Array ();
var s = (new Date ()). valueof ();
for (i=0; i<50000; i++)
{
Array[i]=i;
}
var str = array.join (', ');
var e = (new Date ()). valueof ();
alert (e-s);
}
</script>

In comparison, there are differences between the two methods.

Many cows say that the concatenation of strings by using the array join method in JS is very good. To do this in the project to write a JS class, used to unify the processing string concatenation.

Code

The code is as follows Copy Code

A custom string connection class for stitching strings, which is better than "+" to improve performance
function StringBuffer () {
This._strs = new Array ();
}
StringBuffer.prototype.append = function (str) {
This._strs.push (str);
};
StringBuffer.prototype.arrayToString = function () {
Return This._strs.join ("");
};

And when we use this class, we can go directly to the following methods:

The code is as follows Copy Code

var strbuff=new stringbuffer ();
Strbuff.append ("Hello Www.111cn.net,");
Strbuff.append ("Welcome to javascript!");

Alert (strbuff.arraytostring ());

Now let's take a look at these performance comparisons

  code is as follows copy code


Var Tstart = new Date ();
var str = "";
for (Var i=0;i<10000;i++)
{
str + = "text"
}
var tend = new Date ();
document.write ("The original method plus concatenation of 10,000 strings takes time:" + (Tend.gettime ()-tstart.gettime ()) + "seconds");
var OSB = new StringBuffer ();
Tstart = new Date ();
for (Var i=0;i<10000;i++)
{
Osb.append ("text");
}
var srst = osb.tostring ();
tend = new Date ();
document.write ("<br/>stringbuffer stitching 10,000 Strings takes time:" + (Tend.gettime ()-tstart.gettime ()) + "seconds");

Perhaps you have guessed, StringBuffer is faster than +, in the end how much faster? My test results:
JS Code
FF3.0.10
Original method plus concatenation of 10,000 strings takes time: 3 Hao seconds
StringBuffer stitching 10,000 Strings takes time: 8 Hao seconds
IE7
Original method plus concatenation of 10,000 strings takes time: 15 Hao seconds
StringBuffer stitching 10,000 strings takes time: 16 Hao seconds
IE8
Original method plus concatenation of 10,000 strings takes time: 15 Hao seconds
StringBuffer stitching 10,000 strings takes time: 16 Hao seconds
Chrome1.0.154.46
Original method plus concatenation of 10,000 strings takes time: 1 hao seconds
StringBuffer stitching 10,000 Strings takes time: 2 hao seconds

Here is a performance test to speak with the facts!

The code is as follows Copy Code

function Xntest () {
var d1=new Date ();
var str= "";
for (Var i=0;i<10000;i++) {
    str+= "Stext"; br>}
Var d2=new Date ();
document.write ("string concatenation takes time:" + (D2.gettime ()-d1.gettime ()) + "milliseconds;");

D1=new Date ();
var sb=new StringBuilder ();
for (Var i=0;i<10000;i++) {
    sb.append ("Stext");
}
var result=sb.tostring ();
D2=new Date ();
document.write ("Array mode time consuming:" + (D2.gettime ()-d1.gettime ()) + "milliseconds;");
}

 /////uses array-implemented string concatenation functions to facilitate C # developers to specifically name Stringbuilde for easy understanding

Function StringBuilder () {
    this._strings_=new Array;
}
Stringbuilder.prototype.append=function (str) {
    this._strings_.push (str);
};
Stringbuilder.prototype.tostring=function () {
    return This._strings_.join ("");
};


The result of the three execution of the Xntest () function is:

string concatenation takes time: 735 milliseconds; array mode is time-consuming: 62 milliseconds;

string concatenation takes time: 766 milliseconds; array mode is time-consuming: 63 milliseconds;

string concatenation takes time: 703 milliseconds; array mode is time-consuming: 63 milliseconds;

This example is stitching 10,000 times string performance test, I believe that the results are obvious to all, interested friends can test their own.

Therefore, in the foreground development we should also try to avoid large-scale string concatenation operation, should use the array method to reasonably improve the code efficiency

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.