【jQuery】jQuery官方基本教程的学习笔记6-性能Performance
2017-12-26 10:29
627 查看
一、Append Outside of loops在循坏外添加
https://jsperf.com/性能测试test1.bad
$.each( myArray, function( i, item ) { var newListItem = "<li>" + item + "</li>"; $( "#ballers" ).append( newListItem ); });
2.good
1)使用document fragment
var frag = document.createDocumentFragment(); $.each( myArray, function( i, item ) { var newListItem = document.createElement( "li" ); var itemText = document.createTextNode( item ); newListItem.appendChild( itemText ); frag.appendChild( newListItem ); }); $( "#ballers" )[ 0 ].appendChild( frag );
2)使用字符串追加
var myHtml = "";
$.each( myArray, function( i, item ) {myHtml += "<li>" + item + "</li>";
});
$( "#ballers" ).html( myHtml );
二、Cache Length During Loops在循环前缓存长度
var myLength = myArray.length;for ( var i = 0; i < myLength; i++ ) {
// do stuff
}
三、Detach Elements to work with them
var table = $( "#myTable" );var parent = table.parent();
table.detach();
// ... add lots and lots of rows to table
parent.append( table );
四、Don't Act on Absent Elements不要执行缺失的元素
// Bad: This runs three functions before it// realizes there's nothing in the selection
$( "#nosuchthing" ).slideUp();
// Better:
var elem = $( "#nosuchthing" );
if ( elem.length ) {
elem.slideUp();
}
// Best: Add a doOnce plugin.
jQuery.fn.doOnce = function( func ) {
this.length && func.apply( this );
return this;
}
$( "li.cartitems" ).doOnce(function() {
// make it ajax! \o/
});
五、Optimize Selectors优化选择器
1.jQuery Extensions
// Slower (the zero-based :even selector is a jQuery extension)$( "#my-table tr:even" );
// Better, though not exactly equivalent
$( "#my-table tr:nth-child(odd)" );
2.Avoid Excessive Specificity
$( ".data table.attendees td.gonzalez" );// Better: Drop the middle if possible.
$( ".data td.gonzalez" );
3.ID-Based Selectors基于ID选择器
// Fast:$( "#container div.robotarm" );
// Super-fast:
$( "#container" ).find( "div.robotarm" );
4.Tips for Older Browsers
// Unoptimized:$( "div.data .gonzalez" );
// Optimized:
$( ".data td.gonzalez" );
5.避免使用全选
$( ".buttons > *" ); // Extremely expensive.$( ".buttons" ).children(); // Much better.
$( ":radio" ); // Implied universal selection.
$( "*:radio" ); // Same thing, explicit now.
$( "input:radio" ); // Much better.
六、Use Stylesheets for Changing CSS on Many Elements使用样式表改变css
// Fine for up to 20 elements, slow after that:$( "a.swedberg" ).css( "color", "#0769ad" );
// Much faster:超过20个元素的话,使用这个方法可能会提高60%的效率
$( "<style type=\"text/css\">a.swedberg { color: #0769ad }</style>")
.appendTo( "head" );
七、Don't Treat jQuery as a Black Box使用源码
https://code.jquery.com/jquery/相关文章推荐
- 【jQuery】jQuery官方基本教程的学习笔记4-异步Ajax
- 【jQuery】jQuery官方基本教程的学习笔记7-代码组织Code Organization
- 【jQuery】jQuery官方基本教程的学习笔记8-界面库jQuery UI
- 【jQuery】jQuery官方基本教程的学习笔记5-插件Plugins
- 【jQuery】jQuery官方基本教程的学习笔记2-事件Events
- 【jQuery】jQuery官方基本教程的学习笔记3-动画效果Effects
- google 官方教程学习之UI性能优化( Analyzing UI Performance with Systrace)
- JQUERY1.9学习笔记 之基本过滤器(十一) 奇数选择器
- 韩顺平_php从入门到精通_视频教程_第2讲_html运行原理②_html文件基本结构_html元素和属性_学习笔记_源代码图解_PPT文档整理
- jquery1.9学习笔记 之选择器(基本元素二)
- jQuery官方基础教程笔记
- [原]Java程序员的JavaScript学习笔记(7——jQuery基本机制)
- 学习ApacheHttpComponents,HttpCore第一部分,基本翻译官方教程
- 【Android官方Training教程】Getting Started部分学习笔记
- JQuery学习笔记-基本选择器
- JQUERY1.9学习笔记 之基本过滤器(十二) 根元素选择器
- jQuery学习笔记(一) 取值、赋值的基本方法
- jquery1.9学习笔记 之选择器(基本元素五)
- jQuery学习笔记——JQuery所支持的基本CSS选择器
- Query Performance Prediction (查询性能预测)学习笔记(一)