Efficiency test _javascript techniques for global variables, function variables, and constant expressions in JavaScript

Source: Internet
Author: User
Tags constant garbage collection

<ptml> <body> <script type= "Text/javascript" > var r, s = "This is a very very loooooooooooooooooooo Ooooooooooooooooooooooooooooooooooooooooooooooong string ", x =/^\s+|\s+$/g; Document.writeln ("string test"); (function () {document.write ("Global-> Global:"); var t = new Date (); for (var i = 0; i < 3000000; i++) {R = s; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("Global-> Local:"); var s = window.s; var t = new Date (); for (var i = 0; i < 3000000; i++) {R = s; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("constant-> Local:"); var R; var t = new Date (); for (var i = 0; i < 3000000 i++) {r = ' This is a very very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooo Ooooooooong string "; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("Property-> Local:"); var R; var s = {0:WINDOW.S}; var t = new Date (); For (Vari = 0; i < 3000000; i++) {r = s[0]; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("local-> Local:"); var R; var s = window.s; var t = new Date (); for (var i = 0; i < 3000000; i++) {R = s; } document.write (New Date ()-T); Document.writeln (""); })(); Document.writeln ("Regular expression test"); (function () {document.write ("Global-> Global:"); var t = new Date (); for (var i = 0; i < 3000000; i++) {r = x; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("Global-> Local:"); var x = window.x; var t = new Date (); for (var i = 0; i < 3000000; i++) {r = x; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("constant-> Local:"); var R; var t = new Date (); for (var i = 0; i < 3000000; i++) {r =/^\s+|\s+$/g; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("Property-> Local:"); var R; var x = {0:window.x}; VAr t = new Date (); for (var i = 0; i < 3000000 i++) {r = x[0]; } document.write (New Date ()-T); Document.writeln (""); })(); (function () {document.write ("local-> Local:"); var R; var x = window.x; var t = new Date (); for (var i = 0; i < 3000000; i++) {r = x; } document.write (New Date ()-T); Document.writeln (""); })(); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Note: In IE, you need to change the "Maximum number of script steps" for IE in this test:
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\styles]
"Maxscriptstatements" =dword:ffffffff
In that case, the efficiency of using a local variable within a function is much higher than a global variable or a constant expression. And the efficiency of the attributes is terrible, and in Firefox and Safari it's a lot slower than taking advantage of global variables.
It is faster to use string constants directly than to take advantage of global variables, but creating regular expressions is much slower than using global variables.
The observation numbers also reveal: In Google Chrome, opera and IE, the use of attributes and the use of global variables is not the difference (opera performance may be the result of its garbage collection mechanism), from this phenomenon, the three browsers in the so-called global variables may be the root of the global object properties.

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.