With more and more web design application of JavaScript technology, and browser JavaScript engine running speed has become a major browser performance indicators, the browser vendors are claiming that their browser faster, hoping to stir the existing competitive situation. The JavaScript speed of the IE8 browser is significantly higher than the IE7 and IE6, but there is a gap between the browsers of the WebKit engine and the Chrome browser's performance in last year's ZDNet JavaScript test. Beat Firefox, Safari and Microsoft's IE browsers. But because of the relatively large use of IE browsers, it is necessary for IE browser JavaScript to raise speed.
We know that the browser executes the script from the inside out at the time of execution, so the global object farthest from our script is likely to be accessed across several levels of scope. But in IE browsers, it takes a lot more time to get from the outermost layer to the outer layer. Plus, JavaScript is a glue language that must be called DOM pairs to accomplish most of our choices. The most famous is the selection element (Document.getelementbyid,document.getelementsbytagname,docuemnt.evaluate, Document.queryselector), creating elements (Document.createelement), in addition to Document.body,document.defaultview.getcomputedstyle, and so on, frequently calling Docum The Ent object, but the document is located under the Window object, so the journey is further away. We have to keep them in a local variable, so it saves them a long journey. The use of this technology is clearly embodied in the jquery Source:
(Function (window, undefined) {
//Define a local copy of jquery
var jquery = function (selector, context) {
//the JQuery object is actually just the Init constructor ' enhanced ' return
to New JQuery.fn.init (selector, context);
},
//map over jQuery into case of overwrite
_jquery = window.jquery,
//Map over the $ in case of Overwrit E
_$ = window.$,
//use of the correct document accordingly with Window argument (sandbox)
document = Window.do Cument,
//==================== province =================
}
//Expose JQuery to the global object
Window.jquery = window.$ = JQuery;
}) (window);
Pass the window into the closure and save it from looking for Windows every time.
Look at other class libraries
Raphael
window. Raphael = (function () {
var separator =/[,]+/,
elements =/^ (circle|rect|path|ellipse|text|image) $/,
Doc = document,
win = window,
//************ slightly **************
Dojo
D.global = this;
EXT
DOC = document,
YUI
//************ slightly ************
} else if (i = = ' win ') {
c[i] = O[i].contentwindow | | o[i];
C.doc = c[i].document;
Slightly ************
y.config = {
Win:window | | {},
doc:document,
But if you didn't introduce the class library, what if IE's JavaScript ran faster? Use a variable to store it? In a foreign blog to see a very powerful hijacking technology, steal Dragon turn Phoenix to the global variable document into a local variable.
/* @cc_on _d=document;eval (' var document=_d ') @*/
<!doctype html>
<meta charset= "Utf-8"/>
<title>javascript speed-up Technology by Masaki </title>
<script type= "Text/javascript" >
var date = new Date;
for (var i = 0; i < 100000; i++) document;
Alert (new date-date);
</script>
<body>
</body>
After using speed-increasing technology:
<!doctype html>
<meta charset= "Utf-8"/>
<title>javascript speed-up Technology by Masaki </title>
<script type= "Text/javascript" >
/* @cc_on _d=document;eval (' var document=_d ') @*/
var date = new Date;
for (var i = 0; i < 100000; i++) document;
Alert (new date-date);
</script>
<body>
!!!!!!!!
</body>
After testing, using the speed-increasing technology, IE performance comparison
ie6 |
|
document |
document.getElementById |
document.title |
|
4" |
1110 |
1219 |
after using speed-increasing technology |
109 |
609 |
656 |
IE8 |
|
document |
document.getElementById |
document.title |
No use of speed-increasing technology |
468 |
797 |
843 |
After using speed-increasing technology |
78 |
328 |
407 |
Let's look at the implementation principle:
Document;
Doc; Obviously, the call was faster than the direct document, and document had to get inside the window to find a
How do you hijack it?
var doc = document;
var document = doc;
That's obviously not going to work. Because in the precompilation phase, the Var variable is in advance, and the code above is equivalent
var doc
var document/// here was hijacked
doc = document/Note that document has become undefined
document = doc// Equivalent to window.undefined = undefined
There is no way to define this document variable during the execution period, and JavaScript's dynamic parsing technology comes in handy, and Eval is one of its representatives.
var doc = document;
Eval (' var document = Doc ');
In order to make IE special, use IE specific conditions to compile.
/* @cc_on
var doc = document;
Eval (' var document = Doc ');
@*/
Well, window stuff is actually quite a lot, so how about we turn them into local variables?
/* @cc_on
eval (function (props) {
var code = [];
for (var i = 0 L = props.length;i<l;i++) {
var prop = props[i];
Window[' _ ' +prop]=window[prop];
Code.push (prop+ ' =_ ' +prop)
} return
' var ' +code.join (', ');
} (' Document event body location title self Top parent alert setinterval clearinterval settimeout cleartimeout '. Split (')) ;
@*/
We can expand it to allow more global variables or global methods to be localized. However, the experience, FF use it will be an error, chrome is slow, other browsers are not obvious.
if (!+ "v1" ) {
var code = [],ri = 0,prop,str = "var" for
(var a in window)
code[ri++] = A;
for (var i = 0, n = code.length;i<n;i++) {
var prop = code[i]
window[' _ ' +prop] = Window[prop];
str = prop+ ' =_ ' +prop+ ', "
}
str = Str.slice (0,-1);
eval (str)
}
<!doctype html>
<meta charset= "Utf-8"/>
<title>javascript speed-up Technology by Masaki </title>
<script type= "Text/javascript" >
var __chrome = navigator.userAgent.indexOf ("Chrome")!==-1;
var __firefox =!!window.components
if (!__chrome &!__firefox) {
var code = [],ri = 0,prop,str = "var"
For (var a in window)
code[ri++] = A;
for (var i = 0, n = code.length;i<n;i++) {
var prop = Code[i]
Window[' _ ' +prop] = Window[prop];
str = prop+ ' =_ ' +prop+ ', '
}
str = Str.slice (0,-1);
eval (str)
}
var date = new Date;
for (var i = 0; i < 100000; i++)
Document
Alert (new date-date);
</script>
<body>
!!!!!!
</body>
Article Source: http://www.cnblogs.com/rubylouvre/archive/2010/02/11/1667628.html
Although IE8 's announcement did not surprise us much, according to Microsoft's published IE9 evaluation of the various performance information, surprised to find, IE9 in the JavaScript engine in the Sunspider test, IE9 significantly beyond IE7 and IE8, and with Chrome, The JavaScript runs in Safari and Firefox browsers are basically the same level. Hope that Microsoft will not repeat the mistakes of IE8, after all, Microsoft is now out of the new browser is more and more diligent, remember the current IE6, do not know how many years, just heard of the IE7, until now IE8 beyond the IE6 become the world's most popular browser version, We look forward to Microsoft IE9 Browser can let competitors take notice.