JQuery optimization - something wrong with maintaining reference to jquery object?

I have a web application that is very dependent on jquery / javascript and its sometimes a little slow. One of the things that are often done is changes to the grid (jqGrid), which means every time I use jQuery to select this object, that is:

function load_editor() { $('#listview').jqGrid(do_stuff); }

I believe that just storing a link to $ ('# listview') - since using it in half a dozen functions - would be faster. So, is there a flaw in the setup:

listview = $('#listview');
function load_editor() { listview.jqGrid(do_stuff); }

It would seem that ordinary objects are already in memory and will not be subjected to a fine search with every use. Is there a drawback to structuring in this way?

(I know in my examples, I throw away the global one. Its all beautifully encapsulated in an object in the actual project, I just use these examples to illustrate my point.)

+3
source share
2 answers

Absolutely you should .

And I highly recommend that you follow the style of the related article - name the jQuery object variables with a prefix $. Knowing which of your variables are jQuery objects, just by looking at them, will be very useful for you when your project gets big.

+4
source

, . , Sizzle , - , , , .

, DOM , , DOM.

+2

Source: https://habr.com/ru/post/1735953/


All Articles