Javascript string Optimization

Source: Internet
Author: User

I haven't written it for a long time.

Today, I saw a long post about string connection optimization.

So I tested it myself. Write a simpleCode

 <!  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; charsets = 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;

// Show 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 " );
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 = "Click me" 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)

Array time used 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.

FF

 
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:

 
1: 1312101715553
Time 2 1312101715622
Time 3 1312101715749
Array time 69
STR connection time: 127

Ie result

 
Array time used 700
STR connection time: 500

FF

 
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.