Should I cache $(this) in jQuery if it is used more than once?

后端 未结 4 1599
故里飘歌
故里飘歌 2020-12-11 05:20

I know that you are supposed to cache the results of a selector if you use it more than once. An example would be:

var $selected = $(\'.some-selected-elemen         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 06:13

    jsperf.com is a great resource - and one I've taken to frequenting of late - for evaluating JavaScript performance. For example, the following test evaluates the performance of cached jQuery elements to that of non-cached:

    http://jsperf.com/jquery-cache-vs-no-cache

    The results echo the answers in this thread.

提交回复
热议问题