Pointers for better performance
JQuery cache objects
Ex: var header = $('#header');
Consider the alternatives:
- jQuery.each ()
- .show ()
- .hide ()
- .toggle ()
Switching to display:none
is much faster. Maybe just use addClass('hidden')
and removeClass('hidden')
Minimize the use of slow jQuery O methods (N ^ 2)
The following methods are also expensive:
- Append ()
- before the name ()
- before()
- after()
The process of these manipulation methods is as follows: cleaning the input string, converting the string into a DOM fragment and entering it into the DOM.
Selector Optimization:
In execution order:
- id (due to uniqueness)
- tag
- name, class (both require verification of the attributes of each DOM element)
To get a specific option, it is preferable to choose a parent identifier to start with:
$('#test p.description').removeClass('hidden'); instead of $('.description').removeClass('hidden');
Use the child selector, where possible, instead of the child selector:
$('div > p').hide(); or $('div').children('p'); instead of $('div p').hide(); or $('div').find('p');
Use contextual search:
$('div').find('p'); instead of $('div', 'p');
Use .filter () instead of using tag selectors:
$('div.name').filter(':input'); instead of $('div.name :input');
memorization:
var myFunc = function (param) { if (!myFunc.cache.hasOwnProperty(param)) { var result = {};
source share