[url]http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html[/url]这篇文章很有意思。
这种创建script标签并且设置script.text以动态执行JavaScript代码的方法,在IE下与eval和window.execScript的性能差不多,但是[b]在firefox3下性能比eval平均高2.4倍[/b]。
这种创建script标签并且设置script.text以动态执行JavaScript代码的方法,在IE下与eval和window.execScript的性能差不多,但是[b]在firefox3下性能比eval平均高2.4倍[/b]。
// Evalulates a script in a global context
globalEval: function( data ) {
if ( data && /\S/.test(data) ) {
// Inspired by code by Andrea Giammarchi
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");
script.type = "text/javascript";
if ( jQuery.support.scriptEval )
script.appendChild( document.createTextNode( data ) );
else
script.text = data;
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709).
head.insertBefore( script, head.firstChild );
head.removeChild( script );
}
},
Firefox中提升JS性能技巧
本文介绍了一种在Firefox浏览器中提高JavaScript性能的方法:通过创建并操作script标签来执行动态JavaScript代码,相较于eval函数,该方法性能提升了2.4倍。此技术适用于需要频繁动态执行代码的场景。
786

被折叠的 条评论
为什么被折叠?



