jQuery性能的优化建议 2022-03-28 07:28 552阅读 0赞 一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些。找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来。我也做了一个jQuery性能优化的简明样式表,你可以打印出来或者设为桌面背景。 **一、选择器性能优化建议** **1. 总是从\#id选择器来继承** 这是jQuery选择器的一条黄金法则。jQuery选择一个元素最快的方法就是用ID来选择了。 <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#content'</code><code>).hide();</code></span></span> </div> </div> </td> </tr> </tbody> </table> 或者从ID选择器继承来选择多个元素: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#content p'</code><code>).hide();</code></span></span> </div> </div> </td> </tr> </tbody> </table> **2. 在class前面使用tag** jQuery中第二快的选择器就是tag选择器(如$(‘head’)),因为它和直接来自于原生的Javascript方法getElementByTagName()。所以最好总是用tag来修饰class(并且不要忘了就近的ID) <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>receiveNewsletter = $(</code><code>'#nslForm input.on'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> jQuery中class选择器是最慢的,因为在IE浏览器下它会遍历所有的DOM节点。尽量避免使用class选择器。也不要用tag来修饰ID。下面的例子会遍历所有的div元素来查找id为’content’的那个节点: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>content = $(</code><code>'div#content'</code><code>); </code><code>// 非常慢,不要使用</code></span></span> </div> </div> </td> </tr> </tbody> </table> 用ID来修饰ID也是画蛇添足: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>traffic_light = $(</code><code>'#content #traffic_light'</code><code>); </code><code>// 非常慢,不要使用</code></span></span> </div> </div> </td> </tr> </tbody> </table> **3. 使用子查询** 将父对象缓存起来以备将来的使用 <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">4</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>header = $(</code><code>'#header'</code><code>); </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>menu = header.find(</code><code>'.menu'</code><code>); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 或者 </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>menu = $(</code><code>'.menu'</code><code>, header);</code></span></span> </div> </div> </td> </tr> </tbody> </table> [来看个例子][Link 1] **4. 优化选择器以适用Sizzle的“从右至左”模型** 自版本1.3之后,jQuery采用了Sizzle库,与之前的版本在选择器引擎上的表现形式有很大的不同。它用“从左至右”的模型代替了“从右至左”的模型。确保最右的选择器具体些,而左边的选择器选择范围较宽泛些: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>linkContacts = $(</code><code>'.contact-links div.side-wrapper'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> 而不要使用 <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>linkContacts = $(</code><code>'a.contact-links .side-wrapper'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> **5. 采用find(),而不使用上下文查找** .find()函数的确快些。但是如果一个页面有许多DOM节点时,需要来回查找时,可能需要更多时间: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>divs = $(</code><code>'.testdiv'</code><code>, </code><code>'#pageBody'</code><code>); </code><code>// 2353 on Firebug 3.6 </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>divs = $(</code><code>'#pageBody'</code><code>).find(</code><code>'.testdiv'</code><code>); </code><code>// 2324 on Firebug 3.6 - The best time </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>divs = $(</code><code>'#pageBody .testdiv'</code><code>); </code><code>// 2469 on Firebug 3.6</code></span></span> </div> </div> </td> </tr> </tbody> </table> **6. 利用强大的链式操作** 采用jQuery的链式操作比缓存选择器更有效: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">4</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'li.menu-item'</code><code>).click(</code><code>function</code> <code>() {alert(</code><code>'test click'</code><code>);}) </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code> </code><code>.css(</code><code>'display'</code><code>, </code><code>'block'</code><code>) </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code> </code><code>.css(</code><code>'color'</code><code>, </code><code>'red'</code><code>) </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code> </code><code>fadeTo(</code><code>2</code><code>, </code><code>0.7</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> **7. 编写属于你的选择器** 如果你经常在代码中使用选择器,那么扩展jQuery的$.expr\[':'\]对象吧,编写你自己的选择器。下面的例子中,我创建了一个abovethefold选择器,用来选择不可见的元素: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">4</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">5</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">6</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$.extend($.expr[</code><code>':'</code><code>], { </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>abovethefold: </code><code>function</code><code>(el) { </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code><strong>return</strong></code> <code>$(el).offset().top < $(window).scrollTop() + $(window).height(); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">} </span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">}); </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>nonVisibleElements = $(</code><code>'div:abovethefold'</code><code>); </code><code>// 选择元素</code></span></span> </div> </div> </td> </tr> </tbody> </table> **二、优化DOM操作建议** **8. 缓存jQuery对象** 将你经常用的元素缓存起来: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>header = $(</code><code>'#header'</code><code>); </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>divs = header.find(</code><code>'div'</code><code>); </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>forms = header.find(</code><code>'form'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> **9. 当要进行DOM插入时,将所有元素封装成一个元素** 直接的DOM操作很慢。尽可能少的去更改HTML结构。 <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">4</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">5</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">6</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">7</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">8</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">9</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">10</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">11</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>var</code> <code>menu = </code><code>'<ul id="menu">'</code><code>; </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code><strong>for</strong></code> <code>(</code><code>var</code> <code>i = </code><code>1</code><code>; i < </code><code>100</code><code>; i++) { </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>menu += </code><code>'<li>'</code> <code> + i + </code><code>'</li>'</code><code>; </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">} </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>menu += </code><code>'</ul>'</code><code>; </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#header'</code><code>).prepend(menu); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 千万不要这样做: </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#header'</code><code>).prepend(</code><code>'<ul id="menu"></ul>'</code><code>); </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code><strong>for</strong></code> <code>(</code><code>var</code> <code>i = </code><code>1</code><code>; i < </code><code>100</code><code>; i++) { </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#menu'</code><code>).append(</code><code>'<li>'</code> <code>+ i + </code><code>'</li>'</code><code>); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">}</span></code> </div> </div> </td> </tr> </tbody> </table> **10. 尽管jQuery不会抛出异常,但开发者也应该检查对象** 尽管jQuery不会抛出大量的异常给用户,但是开发者也不要依赖于此。jQuery通常会执行了一大堆没用的函数之后才确定一个对象是否存在。所以在对一个作一系列引用之前,应先检查一下这个对象存不存在。 **11. 使用直接函数,而不要使用与与之等同的函数** 为了获得更好的性能,你应该使用直接函数如$.ajax(),而不要使用$.get(),$.getJSON(),$.post(),因为后面的几个将会调用$.ajax()。 **12. 缓存jQuery结果,以备后来使用** 你经常会获得一个javasript应用对象——你可以用App.来保存你经常选择的对象,以备将来使用: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>App.hiddenDivs = $(</code><code>'div.hidden'</code><code>); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 之后在你的应用中调用: </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>App.hiddenDivs.find(</code><code>'span'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> **13. 采用jQuery的内部函数data()来存储状态** 不要忘了采用.data()函数来存储信息: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#head'</code><code>).data(</code><code>'name'</code><code>, </code><code>'value'</code><code>); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 之后在你的应用中调用: </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'#head'</code><code>).data(</code><code>'name'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> **14. 使用jQuery utility函数** 不要忘了简单实用的jQuery的[utility函数][utility]。我最喜欢的是$.isFunction(), $isArray()和$.each()。 **15. 为HTML块添加“JS”的class** 当jQuery载入之后,首先给HTML添加一个叫”JS”的class <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'HTML'</code><code>).addClass(</code><code>'JS'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> 只有当用户启用JavaScript的时候,你才能添加CSS样式。例如: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">/* 在css中 */</span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">.JS #myDiv{display:none;}</span></code> </div> </div> </td> </tr> </tbody> </table> 所以当JavaScript启用的时候,你可以将整个HTML内容隐藏起来,用jQuery来实现你想实现的(譬如:收起某些面板或当用户点击它们时展开)。而当Javascript没有启用的时候,浏览器呈现所有的内容,搜索引擎爬虫也会勾去所有内容。我将来会更多的使用这个技巧。 [阅读更多相关内容][Link 2] **三、关于优化事件性能的建议** **16. 推迟到$(window).load** 有时候采用$(window).load()比$(document).ready()更快,因为后者不等所有的DOM元素都下载完之前执行。你应该在使用它之前测试它。 **17. 使用Event Delegation** 当你在一个容器中有许多节点,你想对所有的节点都绑定一个事件,delegation很适合这样的应用场景。使用Delegation,我们仅需要在父级绑定事件,然后查看哪个子节点(目标节点)触发了事件。当你有一个很多数据的table的时候,你想对td节点设置事件,这就变得很方便。先获得table,然后为所有的td节点设置delegation事件: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>"table"</code><code>).delegate(</code><code>"td"</code><code>, </code><code>"hover"</code><code>, </code><code>function</code><code>(){ </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code><strong>this</strong></code><code>).toggleClass(</code><code>"hover"</code><code>); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">});</span></code> </div> </div> </td> </tr> </tbody> </table> [阅读更多相关内容][Link 3] **18. 使用ready事件的简写** 如果你想压缩js插件,节约每一个字节,你应该避免使用$(document).onready() <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">4</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">5</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">6</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">7</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">8</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 也不要使用 </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(document).ready(</code><code>function</code> <code>(){ </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 代码 </span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">}); </span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 你可以如此简写: </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>function</code> <code>(){ </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 代码 </span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">});</span></code> </div> </div> </td> </tr> </tbody> </table> **四、测试jQuery** **19. jQuery单元测试** 测试JavaSript代码最好的方法就是人来测试。但你可以使用一些自动化的工具如[Selenium][],[Funcunit][],[QUit][],[QMock][]来测试你的代码(尤其是插件)。我想在另外一个专题来讨论这个话题因为实在有太多要说的了。 **20. 标准化你的jQuery代码** 经常标准化你的代码,看看哪个查询比较慢,然后替换它。你可以用Firebug控制台。你也可以使用[jQuery的快捷函数][Link 1]来使测试变得更容易些: <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 在Firebug控制台记录数据的快捷方式 </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$.l($(</code><code>'div'</code><code>));</code></span></span> </div> </div> </td> </tr> </tbody> </table> <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 获取UNIX时间戳 </span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">$.time();</span></code> </div> </div> </td> </tr> </tbody> </table> <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">3</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">4</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 在Firebug记录执行代码时间 </span></code> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">$.lt(); </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'div'</code><code>); </code></span></span> </div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">$.lt();</span></code> </div> </div> </td> </tr> </tbody> </table> <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 将代码块放在一个for循环中测试执行时间 </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$.bm(</code><code>"var divs = $('.testdiv', '#pageBody');"</code><code>); </code><code>// 2353 on Firebug 3.6</code></span></span> </div> </div> </td> </tr> </tbody> </table> **五、其他常用jQuery性能优化建议** **21. 使用最新版本的jQuery** 最新的版本往往是最好的。更换了版本后,不要忘记测试你的代码。有时候也不是完全向后兼容的。 **22. 使用HMTL5** 新的HTML5标准带来的是更轻巧的DOM结构。更轻巧的结构意味着使用jQuery需要更少的遍历,以及更优良的载入性能。所以如果可能的话请使用HTML5。 **23. 如果给15个以上的元素加样式时,直接给DOM元素添加style标签** 要给少数的元素加样式,最好的方法就是使用jQuey的css()函数。然而更15个以上的较多的元素添加样式时,直接给DOM添加style 标签更有效些。这个方法可以避免在代码中使用硬编码(hard code)。 <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> </td> <td> <div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>$(</code><code>'<style type="text/css"> div.class { color: red; } </style>'</code><code>) </code></span></span> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code>.appendTo(</code><code>'head'</code><code>);</code></span></span> </div> </div> </td> </tr> </tbody> </table> **24. 避免载入多余的代码** 将Javascript代码放在不同的文件中是个好的方法,仅在需要的时候载入它们。这样你不会载入不必要的代码和选择器。也便于管理代码。 **25. 压缩成一个主JS文件,将下载次数保持到最少** 当你已经确定了哪些文件是应该被载入的,那么将它们打包成一个文件。用一些开源的工具可以自动帮你完成,如使用[Minify][](和你的后端代码集成)或者使用[JSCompressor][],[YUI Compressor][] 或 [Dean Edwards JS packer][]等在线工具可以为你压缩文件。我最喜欢的是[JSCompressor][]。 **26. 需要的时候使用原生的Javasript** 使用jQuery是个很棒的事情,但是不要忘了它也是Javascript的一个框架。所以你可以在jQuery代码有必要的时候也使用原生的Javascript函数,这样能获得更好的性能。 **27. 从Google载入jQuery框架** 当你的应用正式上线的时候,请从Google CDN载入jQuery,因为用户可以从最近的地方获取代码。这样你可以减少服务器请求,而用户如果浏览其他网站,而它也使用Google CDN的jQuery时,浏览器就会立即从缓存中调出jQuery代码。 <table> <tbody> <tr> <td> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">1</span> </div> <div> <span style="font-family:'KaiTi_GB2312';font-size:18px;">2</span> </div> </td> <td> <div> <div> <code><span style="font-family:'KaiTi_GB2312';font-size:18px;">// 链接特定版本的压缩代码 </span></code> </div> <div> <span style="font-size:18px;"><span style="font-family:'KaiTi_GB2312';"><code><script type=</code><code>"text/javascript"</code> <code>src=</code><code>"https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"</code><code>></script></code></span></span> </div> </div> </td> </tr> </tbody> </table> **28. 缓慢载入内容不仅能提高载入速度,也能提高SEO优化** (Lazy load content for speed and SEO benefits) 使用Ajax来载入你的网站吧,这样可以节约服务器端载入时间。你可以从一个常见的侧边栏widget开始。 [Link 1]: https://github.com/doomhz/jQuery-Tweaks [utility]: http://docs.jquery.com/Utilities [Link 2]: http://www.learningjquery.com/2008/10/1-awesome-way-to-avoid-the-not-so-excellent-flash-of-amazing-unstyled-content [Link 3]: http://api.jquery.com/delegate/ [Selenium]: http://seleniumhq.org/ [Funcunit]: http://funcunit.com/ [QUit]: http://docs.jquery.com/Qunit [QMock]: https://github.com/andybeeching/qmock [Minify]: http://code.google.com/p/minify/ [JSCompressor]: http://jscompress.com/ [YUI Compressor]: http://developer.yahoo.com/yui/compressor/ [Dean Edwards JS packer]: http://dean.edwards.name/packer/
相关 【Redis】性能优化建议 1.使用连接池 > 【推荐】 使用带有连接池的数据库,可以有效控制连接,同时提高效率,标准使用方式: JedisPoolConfig jedisPoolConf 浅浅的花香味﹌/ 2022年11月01日 15:00/ 0 赞/ 282 阅读
相关 jQuery性能优化 [jQuery性能优化][jQuery] 现在越来越多的人应用jQuery了,有些同学在享受爽快淋漓coding时就将性能问题忽略了, 比如我. jquery虽在诸 た 入场券/ 2022年07月28日 01:47/ 0 赞/ 227 阅读
相关 Android 性能微型优化建议 Android 性能微型优化建议 官方原文:[https://developer.android.com/training/articles/perf-tips.html 骑猪看日落/ 2022年06月07日 02:15/ 0 赞/ 347 阅读
相关 jQuery性能的优化建议 一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些。找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来。我也做了一个jQuery性能 落日映苍穹つ/ 2022年03月28日 07:28/ 0 赞/ 553 阅读
相关 jQuery性能优化大全 作为一个菜鸟前端,对于[jquery][]好用还是相当喜欢的,初步入门,总是向着最容易的地方入手,对于jquery的优化知识了解不多,看到一篇文章写得很好,其实在“jquery 本是古典 何须时尚/ 2022年01月16日 11:09/ 0 赞/ 362 阅读
相关 Web前端性能优化建议 1、HTML CSS JS位置 一般需要将CSS放页面最上面,即HEAD部分,而将JS代码放页面底部。因为页面需要加载为CSS才进行渲染,而JS如果不是在页面加载之前就要执行 今天药忘吃喽~/ 2021年11月23日 07:34/ 0 赞/ 686 阅读
相关 sql性能优化以及性能测试(建议收藏) 笛卡尔连接; 例1; 没有携带on的条件字句,此条slq查询的结构集等价于,a表包含的条数*b表包含的乘积 select * from table a cross jo... 朱雀/ 2021年07月06日 02:11/ 1 赞/ 12060 阅读
相关 jQuery性能优化 前言,最近在写jquery的时候,出现一下性能相关问题,在此特作总结,以待日后多加注意。 1.关于变量的定义 这里不仅仅是jquery,即便是`javascript`都 亦凉/ 2021年07月04日 20:35/ 0 赞/ 517 阅读
相关 jQuery性能优化指南 一、总是从ID选择器开始继承 <!-- 在jQuery中最快的选择器是ID选择器,因为它直接来自于JavaScript的getElementById()方法。如下 浅浅的花香味﹌/ 2021年07月04日 18:03/ 0 赞/ 579 阅读
相关 jQuery性能优化 1,总是从ID选择器开始继承 在jQuery中最快的选择器是ID选择器,因为它直接来自于JavaScript的getElementById()方法。 例如有一段HTM... 系统管理员/ 2021年01月27日 16:33/ 0 赞/ 804 阅读
还没有评论,来说两句吧...