javascript - Is there a performance impact to using the jQuery $() operator many times? -


is there significant difference if construct jquery object around element once or many times? instance:

var jel = $(el); $.each(myarray, function() {     jel.addclass(this); } 

versus:

$.each(myarray, function() {     $(el).addclass(this); } 

i know there other ways write might sidestep issue, question whether should work $(el) once, or if irrelevant. example contrived.

bonus points explaining $(el) behind scenes.

i know theoretically more work being done, don't know whether matters... if jquery caches or browsers @ second request or whatever, not worth it.

fyi: relevant jquery api link here (which provide because $() isn't easiest thing google for): http://api.jquery.com/jquery/#using-dom-elements

also worth including useful link: http://www.artzstudio.com/2009/04/jquery-performance-rules/, several of points center around saving, chaining, , selecting well.

yes, there performance impact.

in first example, 1 instance created.

in second, instance created each iteration of loop.

depending on size of myarray, lead lot of extraneous instances being created chew through memory.


Comments