jQuery’s selector is a very convenient tool, but if the object is not cached, it could cause excessive memory issue. Like the way you normally use for JavaScript, in jQuery, it’s a good practice to use object caching.
For instance, if you’re logging the various properties of the event object for a mousemove event, and the
code lags behind because it uses $(‘.classname’) selectors to find and update HTML with the event data.
Your page contains this HTML code for the log:
$(‘html’).mousemove( function( event ) {
$(‘#log’).html( event.clientX );
});
The code above can be optimized using object caching:
var $log = $(‘#log’);
$(‘html’).mousemove( function( event ) {
$log.html( event.clientX );
});