简体   繁体   English

Internet Explorer的Javascript优化

[英]Javascript optimizations for Internet Explorer

It is well known that the Internet Explorer Javascript Engines are way behind in terms of performance, in particular IE 8 and older, when compared to Chrome, Safari (Webkit) or Firefox (Mozilla). 众所周知,与Chrome,Safari(Webkit)或Firefox(Mozilla)相比,Internet Explorer Javascript引擎在性能方面落后,特别是IE 8及更早版本。

When developing a web application with significant javascript functionality, IE performs much worst than the others. 在开发具有重要javascript功能的Web应用程序时,IE的性能远远低于其他应用程序。

Are there any practices that could help improve your javascript code so the divide between the good performers (non-IE) and the bad performer (IE) is not that wide? 是否有任何可以帮助改善您的javascript代码的做法,因此优秀表现者(非IE)和表现不佳者(IE)之间的差距不是那么大?

One common way to optimize performance is caching the 'max' value in for loops. 优化性能的一种常用方法是在for循环中缓存“max”值。

So, let's say you have to iterate through an array called sampleArray. 所以,假设你必须遍历一个名为sampleArray的数组。 You could optimize the below statement: 您可以优化以下声明:

var sampleArray = [3, 10, 12, 5, 9];

for(var i = 0; i < sampleArray.length; i++){
   var currentElement = sampleArray[i];
  // so something wit element
}

by changing it to be: 通过将其更改为:

for(var i = 0, max = sampleArray.length; i < max; i++){
       var currentElement = sampleArray[i];
      // so something wit element
}

This has shown to show a pretty big performance increase in all browsers. 这表明所有浏览器都有相当大的性能提升。 In particular, IE7 has proven to get a 190 times increase in performance using this pattern (from High Performance Javascript ) 特别是IE7已经证明使用这种模式可以获得190倍的性能提升(来自High Performance Javascript

Another couple of common solutions: 另外两种常见的解决方案:

Cache frequently used DOM nodes , do not recalculate them in the same function again. 缓存经常使用的DOM节点 ,不要再次在同一个函数中重新计算它们。 Eg instead of 而不是

$(id).parentNode.something();
$(id).parentNode.somethingOther();

use 使用

var e = $(id).parentNode;
e.something();
e.somethingOther();

Cache frequently used objects from outer scope. 从外部作用域缓存常用对象 Eg instead of 而不是

if (this.options.type == 'a') {
    // ...
} else if (this.options.type == 'b') {
    // ...
}

use 使用

var type = this.options.type;
if (type == 'a') {
    // ...
} else if (type == 'b') {
    // ...
}

This will have also positive impact on code size before and after minifying. 这将对缩小前后的代码大小产生积极影响。

I think reasonable solution for it is Google Chrome Frame 我认为合理的解决方案是Google Chrome Frame
This just allow use Chrome Frame in IE and don't use it in non-IE browsers. 这只允许在IE中使用Chrome Frame ,不要在非IE浏览器中使用它。

JavaScript performance and development tools for IE: IE的JavaScript性能和开发工具:
Microsoft Support: Internet Explorer Performance Article - http://support.microsoft.com/kb/982891 Microsoft支持:Internet Explorer性能文章 - http://support.microsoft.com/kb/982891

a. 一个。 One way to increase your performance in older versions of IE would be to use RAW JavaScript to create DOM elements instead of jQuery. 提高旧版IE的性能的一种方法是使用RAW JavaScript来创建DOM元素而不是jQuery。

example - 示例 -

(function () {
  var rosterContainer = document.getElementByID("roster");
  var memberContainer = document.createElement("div");

  $(memberContainer).addClass("one_fourth alpha hentry")
                    .attr("id", mName.replace(/\s+/g," "))
                    .appendTo($(rosterContainer));
})();

b. You can also re-engineer your user experience (UX) for IE browsers. 您还可以为IE浏览器重新设计用户体验(UX)。

Example - my site uses heavy JS/jquery plugins to load large backgrounds and show them as a slide show with alpha tweens. 示例 - 我的站点使用大量的JS / jquery插件来加载大背景并将其显示为带有alpha补间的幻灯片放映。 But for my IE users, I don't load the JS or the plugin or the background images - i just show a static titled bg. 但对于我的IE用户,我没有加载JS或插件或背景图像 - 我只是显示一个静态标题bg。

you can use loaders like yepnope.js to load optimized JS for IE 您可以使用像yepnope.js这样的加载器来加载IE的优化JS

c. C。 Make sure your site's UI is fully functional w/o JavaScript. 确保您的网站的UI完全无需JavaScript。 This is always a good place to start. 这始终是一个好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM