<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.