Tips and Tricks to improve JQuery efficiency
* Always use the latest version of jQuery core where possible.
* Use faster selector
- id selector, tag selector > class selector > pseudoclass selector, attribute selector
* Faster way to get child ele from a parent ele.
- $parent.find('.child')
* Don't use jQuery unless it's absolutely necessary
- javascript's original methods are faster.
* Do cache
- var parents = $('.parents'); // caching
{
// bad way
children = $('.parents').find('.child');
others = $('.parents').find('.others');
}
{
// better way, use cache
children = parents.find('.child');
others = parents.find('.others')
}
* Chaining
- e.g. var parents = $('.parents').doSomething().doSomethingElse();
- in this way, jQuery will cache the result of each step automatically,
so it's faster than non-chaining way.
* Use event delegation
- javascript allow events to bubble up the DOM tree to a parent element.
as "<table><tr></tr>...<tr></tr></table>", it's better to bind a single
event handler to <table> element than each <tr> element.
2 ways for delegation:
1) // .delegate() method
$("td").bind("click",function(){
$(this).toggleClass("click");
});
2) // .live() method
$("table").each(function(){
$("td",this).live("click", function(){
$(this).toggleClass("click");
});
});
Difference:
.delegate() fire when event bubble to the specific parent element.
.live() fire when event bubble to the root element.
so .delegate() is a little faster than .live()
* You'd better not to modify the DOM tree
- each DOM insertion is costly. so be careful to use append(),
insertBefore(), insertAfter()
if you want to insert several element items, you can put them together
and use a single append() method
- when you're doing heavy interaction with a node, you should use .detach() method first.
- if you're to attach data to a node, please use $.data(elem,key,value)
//bad
var ele = $('#elem');
ele.data(key, value);
//good, 10 times faster than above one
var ele = $('#elem');
$.data(ele, key, value);
* Understand loops
- unless absolutely necessary, avoid loops.
- javascript's for, while statement is faster than jQuery.each() method
* Avoid constructing new jQuery object unless necessary
- // slower
var $text = $('#text');
var $ts = $text.text();
// faster
var $text = $('#text');
var $ts = $.text($text);
// http://addyosmani.com/jqprovenperformance/
分享到:
相关推荐
Tips and Tricks to Improve CNN-based Chest X-ray Diagnosis A Survey.zip
在当前的医疗影像分析领域,尤其是肺部疾病诊断中,卷积神经网络(CNN)的应用日益广泛。然而,由于胸透(Chest X-ray, CXR)图像的标注数据相对稀少,这往往导致CNN模型在训练过程中容易过拟合,从而影响其在实际...
After the tremendous success of Direct3D ShaderX: Vertex and Pixel Shader Tips and Tricks, I planned to do another book with an entirely new set of innovative ideas, techniques, and algorithms...
Shuffle: Tips and Tricks Julien Demouth, NVIDIAhttp://www.gputechconf.com/page/home.htmlGlossary Warp— Implicitly synchronized group of threads (32 on current HW) Warp ID (warpid)— Identifier ...
《ShaderX Vertex and Pixel Shader Tips and Tricks》是一本专注于计算机图形学中着色器技术的专业书籍。本书由Wolfgang F. Engel编辑,由Wordware Publishing, Inc.出版,并获得了美国国会图书馆的编目数据记录。...
《D3 tips and tricks version 4》是一本专注于Web前端开发的书籍,主要介绍了使用D3.js库进行交互式数据可视化的技术。D3.js是一种非常流行的前端JavaScript库,用于基于Web标准生成动态和交互式的数据可视化图表。...
wxPython Grid Tips and Tricks
This collection of MongoDB tips, tricks, and hacks helps you resolve issues with everything from application design and implementation to data safety and monitoring. You get specific guidance in ...
Some tips and tricks about Windows Ultimate.
Best practice and tips & tricks to write scientific papers in LaTeX, with figures generated in Python or Matlab.zip
Microsoft Visual C# IDE Tips and Tricks
Visual Studio Code tips and tricks for JavaScript developers
### PDF Can be Pretty Darn Fancy: Tips and Tricks for the ODS PDF Destination #### 概述 在SAS软件中,ODS(Output Delivery System)是一个非常强大的工具,它允许用户控制输出格式、样式以及目的地。ODS PDF...
50 Tips and Tricks for MongoDB Developers中文版
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集