When to use Vanilla JavaScript vs. jQuery?

后端 未结 13 2071
独厮守ぢ
独厮守ぢ 2020-11-22 09:08

I have noticed while monitoring/attempting to answer common jQuery questions, that there are certain practices using javascript, instead of jQuery, that actually enable you

13条回答
  •  迷失自我
    2020-11-22 09:28

    If you are mostly concerned about performance, your main example hits the nail on the head. Invoking jQuery unnecessarily or redundantly is, IMHO, the second main cause of slow performance (the first being poor DOM traversal).

    It's not really an example of what you're looking for, but I see this so often that it bears mentioning: One of the best ways to speed up performance of your jQuery scripts is to cache jQuery objects, and/or use chaining:

    // poor
    $(this).animate({'opacity':'0'}, function() { $(this).remove(); });
    
    // excellent
    var element = $(this);
    element.animate({'opacity':'0'}, function() { element.remove(); });
    
    // poor
    $('.something').load('url');
    $('.something').show();
    
    // excellent
    var something = $('#container').children('p.something');
    something.load('url').show();
    

提交回复
热议问题