JavaScript three string connection modes and performance comparisons

Connect “+” connection string

STR = “a”;
STR + = “B”;

This approach is relative to the following, the most convenient and fast. It is recommended that the connection between 100 characters will use this connection method.

The second type: As an intermediary, Jion functions are used to connect

  VAR Arr = new array (); Arr .push (a); arr.push (b); var str = arr.join ("");  
Third type: using an object attribute connection String

function stringConnect () {this._str_ = new array ();} stringconnect.prototype.Append = function (a) {this._str_. Push (a);} stringconnect.prototype.tostring = function () {return “);} var mystr = new stringconnect; mystr.Append (” a “); var str = mystr.tostring ();
  With the following code, the number of three method performance is compared, and the number of connected strings is adjusted by changing the value of C:  
VAR STR = “”; VAR D1, D2; VAR C = 5000; / / Connect the number of strings // —————– —– Test the third partTime-consuming time ——- D1 = new date (); function stringconnect () {this._str_ = new array ();} stringconnect.prototype.Append = function (a) {this._str_.push (a } StringConnect.Prototype.toNect.Prototype.toNNECT.PROTYPE.TOSTRING = Function () {Return this._str_.join (“”);} var mystr = new stringConnect; for (var i = 0; i

 I adjusted C, equal to 5000, 50000, 500,000, 500000, each value 10 times, the last result is as follows:  <c;i++){
    mystr.append("a");
  }
str=mystr.toString();
 d2=new Date();
 console.log(d2.getTime()-d1.getTime());
//-----------------------------------------------------

//------------------------测试第二种方法耗费时间-------
d1=new Date();
  var arr=new Array();
for(var i=0;i<c;i++){
  arr.push("a");
}
str=arr.join("");
  d2=new Date();
console.log(d2.getTime()-d1.getTime());
//-------------------------------------------------------
//------------------------测试第一种方法耗费时间-------
d1=new Date();for(var i=0;i<c;i++){
  str+="a";
}
d2=new Date();
console.log(d2.getTime()-d1.getTime());
//-------------------------------------------------------

 c = 5000 
Average time consumption (unit milliseconds)

Third Special 3 2 2 3 1 2 2 1 1 1.8

Second 1 3 0 3 1 3 4 1 4 2 2.2

First Type 0 0 0 0 0 1 1 1 1 1 0.5

C = 50000

Third 22 12 9 14 12 13 13 13 1017 13.5
Section 8 13 12 8 11 11 8 9 8 9.7
First 7 12 5 11 10 10 13 16 12 10.6

c = 500000

The third 104 70 74 69 78 77 69 102 73 78 78.7
Second 78 100 99 99 98 98 96 71 94 97 93.2
First 90 87 83 85 85 83 84 83 88 86 85.4

C = 5000000

Third species 651 871 465 444 1012 436 787 449 432 444 599.1
Second 568 842 593 747 417 747 719 549 573 563 631.8
First 516 279 616 161 466 416 201 495 510 515 417.5

When the statistical 5000000 adds a random parameter in the address bar, it should be the impact of the cache. From the perspective, the first method is not more consuming more than the other, and even more advantages, this and the instructions on the manual are obviously inconsistent.

Test System: Win 7 Flagship


Browser: Chrome 52.0.2739.0 m

Summary

The above is the three-character string connection mode and performance comparison of the JavaScript introduced to you, I hope that everyone can help, if If you have any questions, please leave a message, the small package will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!


© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment