Javascript string Optimization

Source: Internet
Author: User

Copy codeThe Code is as follows: <! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd">
<Html>
<Head>
<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
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 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 ("\ narray time used ");
Mes. push (arrayMils> = 0? ArrayMils: arrayMils + 1000 );
Mes. push ("\ nstr connection time ");
Mes. push (stringMils> = 0? StringMils: stringMils + 1000 );
Alert (mes. join (""));
}
</Script>
</Head>
<Body>
<Input type = "button" value = "" onclick = "winProps ();"/>
</Body>

The result is not similar to the general statement. Using array will be faster. In ie9, 8. Ff, google, sogou, and Other ie core browsers are tested. It is true that the array method is slower than the "+" method in tests with an order of magnitude over 100000.
1000000 results
Ie for the first time (probably, ie cannot be copied, and the figure will not be truncated)Copy codeThe Code is as follows: array time 890
Str connection time: 450

Ie may be caused by a warning that the memory is not recycled at the second point, and the time is 1400 and 1200. After a few times, the connection time of + = is returned to 400, but the array object is not recycled? Open the task manager and refresh ie again. The memory has dropped by more than 100 mb. You should study the javascript Memory recycle mechanism of ie.
FfCopy codeThe Code is as follows: time 1: 1312102053720
Time 2 1312102053842
Time 3 1312102053882
Array time used 122
Str connection time: 40

Google (cannot copy data)
Array time used 200
Str connection time: 100
Opera
1: 1312102258270
Time 2 1312102258460
Time 3 1312102258592
Array time used 190
Str connection time: 132
By the way, ff throws an exception after the data reaches 5000000, which should be caused by memory overflow... (Ps, not very familiar)
Error: uncaught exception: 8.375396749959738e-274
Test the warning in ie, and then out of memory.
Of course, big data connection is impossible on the current webpage, and data processing should be done in the background. The teacher's host does not dare to install so many browsers
It is actually the jion of array, which consumes too much time. After this sentence is not added, google will show the advantages of array:
Opera results:Copy codeThe Code is as follows: time 1: 1312101715553
Time 2 1312101715622
Time 3 1312101715749
Array time 69
Str connection time: 127
[Code]
Ie result
Array time used 700
Str connection time: 500
Ff
[Code]
1: 1312101925895
Time 2 1312101925945
Time 3 1312101925979
Array time 50
Str connection time 34

Google
Array time 17
Str connection time: 100
Check the jion mechanism and try again.
Ps: browser version
Google12.0.742.122
Ff5.0.1
Opera11.50
Ie8.0.6001.18702
Pss: Poor ie processing speed

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.