First of all, keep in mind that jQuery is javascript. This means we should adopt the same coding conventions, style guides and best practices.
First of all, if you are a JavaScript novice, I recommend you read "24 Best Practices for JavaScript Beginners". This is a high-quality JavaScript tutorial. It is best to read it before contacting jQuery.
When you are ready to use jQuery, I strongly recommend that you follow these guidelines:
1. Caching variables
DOM traversal is expensive, so try to cache reused elements.
h = $('#element').height();
$('#element').css('height',h-20);
// Suggestions
$element = $('#element');
h = $element.height();
$element.css('height',h-20);
$element = $('#element');
h = $element.height();
$element.css('height',h-20);
// Suggestions
var $element = $('#element');
var h = $element.height();
$element.css('height',h-20);
var first = $('#first');
var second = $('#second');
var value = $first.val();
// Suggestion - Prefix the jQuery object with $
var $first = $('#first');
var $second = $('#second'),
var value = $first.val();
$first.click(function(){
$first.css('border','1px solid red');
$first.css('color','blue');
});
$first.hover(function(){
$first.css('border','1px solid red');
})
// Recommendation
$first.on('click',function(){
$first.css('border','1px solid red');
$first.css(' color','blue');
})
$first.on('hover',function(){
$first.css('border','1px solid red');
})
$first.click(function(){
$first.css('border','1px solid red');
$first.css('color','blue');
});
// Suggestions
$first.on('click',function(){
$first.css({
'border':'1px solid red',
'color':'blue'
});
});
$second.html(value);
$second.on('click',function(){
alert('hello everybody');
});
$second. fadeIn('slow');
$second.animate({height:'120px'},500);
// Suggestions
$second.html(value);
$second.on('click',function(){
alert('hello everybody');
}).fadeIn('slow') .animate({height:'120px'},500);
$second.html(value);
$second.on('click',function(){
alert('hello everybody');
}).fadeIn('slow') .animate({height:'120px'},500);
// Suggestions
$second.html(value);
$second
.on('click',function(){ alert('hello everybody');})
.fadeIn('slow')
.animate({height:'120px'},500);
function initVar($myVar) {
if(!$myVar) {
$myVar = $('#selector');
}
}
// Suggestions
function initVar($myVar) {
$myVar = $myVar || $('#selector');
}
if(collection.length > 0){..}
// Suggestions
if(collection.length){..}
var
$container = $("#container"),
$containerLi = $("#container li"),
$element = null;
$element = $containerLi.first();
//... Many complex operations
// better
var
$container = $("#container"),
$containerLi = $container.find("li"),
$element = null;
$element = $containerLi.first().detach();
//... Many complex operations
$container.append($element);
$('#id').data(key,value);
// Suggestions (efficient)
$.data('#id',key,value);
var
$container = $('#container'),
$containerLi = $('#container li'),
$containerLiSpan = $('#container li span');
// Suggestions (efficient)
var
$container = $('#container '),
$containerLi = $container.find('li'),
$containerLiSpan= $containerLi.find('span');
$('.container > *');
// Suggestions
$('.container').children();
Avoid implicit universal selectors
Universal selectors are sometimes implicit and difficult to find.
// Oops
$('.someclass :radio');
// Suggestions
$('.someclass input:radio');
Optimize selectors
For example, the Id selector should be unique, so there is no need to add additional selectors.
// Oops
$('div#myid');
$('div#footer a.myLink');
// Recommendation
$('#myid');
$('#footer .myLink');
$('#outer #inner');
// Suggestions
$('#inner');
Abandon deprecated methods
It is very important to pay attention to deprecated methods with each new version and try to avoid using these methods.
$('#stuff').live('click', function() {
console.log('hooray');
});
// Suggestion
$('#stuff').on('click', function() {
console.log('hooray');
});
18. Combine jQuery and javascript native code when necessary
As mentioned above, jQuery is javascript, which means that anything you can do with jQuery can also be done with native code. Native code (or vanilla) may not be as readable and maintainable as jQuery, and the code is longer. But it also means more efficient (usually the closer to the underlying code the less readable the higher the performance, for example: assembly, which of course requires more powerful people). Remember that no framework can be smaller, lighter, and more efficient than native code
Given the performance differences between vanilla and jQuery, I strongly recommend absorbing the best of both worlds and using (if possible) native code equivalent to jQuery.
19. Final advice
Finally, the purpose of recording this article is to improve the performance of jQuery and some other good suggestions. If you want to delve deeper into this topic you will find a lot of fun. Remember, jQuery is not required, just an option. Think about why you want to use it. DOM manipulation? ajax? stencil? css animation? Or a selector engine? Perhaps a javascript microframework or a customized version of jQuery would be a better choice.