What is the cost of '$(this)'?

Asked
Active3 hr before
Viewed126 times

1 Answers

90%

The jQuery function $() is expensive. Calling it repeatedly is extremely inefficient.,In the jQuery tag info this warning appears: ,Then if the string is a selector (other than id), jQuery traverses the DOM to find a match with its expensive find function:, When buying a property, is your agent's incentive contrary to yours?

Well... that is true only for string selectors, which get parsed with regex to find out what they are:

quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/

Then if the string is a selector (other than id), jQuery traverses the DOM to find a match with its expensive find function:

} else if (!context || context.jquery) {
   return (context || rootjQuery).find(selector);
}

If we pass a DOMElement, the only action jQuery does is saving the DOMElement parameter as the context of the newly created jQuery object and setting the length of the context to 1:

// Handle $(DOMElement)
if (selector.nodeType) {
   this.context = this[0] = selector; // Selector here is a DOMElement
   this.length = 1;
   return this;
}
load more v