JavaScript string string optimization problem _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>test</title>
<script type= "Text/javascript" >
function Winprops () {
var message = [];
var str;
var data = new Date ();
var mils1 = Data.gettime ();
Array arrays
for (var i = 0; i<500000;i++)
Message.push ("This window is");
Message.join ("");
data = new Date ();
var mils2 = Data.gettime ();
Arraymils = MILS2-MILS1;
String Direct connection
for (var i = 0; i<500000;i++)
str+= "This window is";
data = new Date ();
var mils3 = Data.gettime ();
STRINGMILS=MILS3-MILS2;
Display content, not important
var mes = [];
Mes.push ("Time 1:");
Mes.push (MILS1);
Mes.push ("\ n time 2");
Mes.push (MILS2);
Mes.push ("\ n time 3");
Mes.push (MILS3);
Mes.push ("Time spent \narray");
Mes.push (arraymils>=0?arraymils:arraymils+1000);
Mes.push ("\nstr connection Time");
Mes.push (stringmils>=0?stringmils:stringmils+1000);
Alert (Mes.join (""));
}
</script>
<body>
<input type= "button" value= "dot i" onclick= "winprops ();" />
</body>

The result is not like the general saying, use array will be quick. In ie9,8. Ff,google, Sogou and other IE core browser under the test, indeed in the order of magnitude more than 100000 of the test array method than the "+" method will slow.
1 million times of results
ie the first time (probably, ie can not copy, the picture is not cut)
Copy Code code as follows:

Array Time 890
STR connection Time 450

IE may be memory does not recycle the second point will pop-up warning, and time to use 1400 and 1200, point more than a few times, + + connection way time to return to 400, baffled is the array object is not recycled? Open Task Manager, and then refresh the Internet Explorer, memory down more than 100 m, you should study IE JavaScript memory recovery mechanism.
Ff
Copy Code code as follows:

Time 1:1,312,102,053,720
Time 2 1312102053842
Time 3 1312102053882
Array Time 122
STR Connection Time 40

Google (also can't copy data)
Array Time 200
STR Connection TIME 100
Opera
Time 1:1,312,102,258,270
Time 2 1312102258460
Time 3 1312102258592
Array Time 190
STR connection Time 132
By the way, after the data to 5000000 FF throws an exception, should be memory overflow ... (PS, not very understand)
Error: Uncaught exception:8.375396749959738e-274
In IE test the decisive warning, and then out of the memory.
Of course how large data connections, in the current Web page is not possible, data processing should be done in the background. The teacher's mainframe is afraid to install so many browsers
is actually the array of jion, too time-consuming. After not adding this sentence, Opera,google shows the advantages of array:
Opera results:
Copy Code code as follows:

Time 1:1,312,101,715,553
Time 2 1312101715622
Time 3 1312101715749
Array Time 69
STR connection Time 127
[Code]
IE results
Array Time 700
STR Connection Time 500
Ff
[Code]
Time 1:1,312,101,925,895
Time 2 1312101925945
Time 3 1312101925979
Array Time 50
STR connection Time 34

Google
Array Time 17
STR Connection TIME 100
Go check the jion mechanism and write it.
PS: Browser version
google12.0.742.122
ff5.0.1
opera11.50
ie8.0.6001.18702
Pss:ie processing speed does not give force

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.