Does jQuery do any kind of caching of “selectors”?

后端 未结 14 2196
挽巷
挽巷 2020-11-27 18:14

For example, will the first piece of code perform a full search twice, or is it smart enough to cache results if no DOM changes have occurred?

if ($(\"#navba         


        
14条回答
  •  清歌不尽
    2020-11-27 18:47

    jQuery doesn't, but there's the possibility of assigning to variables within your expression and then use re-using those in subsequent expressions. So, cache-ifying your example ...

    if ((cached = $("#navbar .heading")).text() > "") {
      cached.hide();
    }
    

    Downside is it makes the code a bit fuglier and difficult to grok.

提交回复
热议问题