.filter (': last') vs .last ()

I was wondering if there is a difference between .filter(':last') and .last() ?

It seems to me that they are doing the same thing, but I'm new to jQuery. If there is no difference in the results, which one is recommended or is it just a matter of personal preference?

+4
source share
2 answers

last works by saying "give me the last item from the selection". This requires only two function calls and four lines of code. This cannot be done faster.

filter(':last') , however, is much more complicated. This is a much more flexible system that allows you to return multiple items if you want, or multiple conditions, or a mixture of them. It is much less effective because it has to work out what you want. For example, parsing ':last' takes a little time, while using the last function is a simple property search.

last much more effective.

+5
source

:last - selects the last matched item.

last() - Reduce the set of matched elements to the end in the set.

As you can see, they do the same (in terms of the final result, anyway).

last() slightly faster than: last (although you may not notice it, it's always good to know).

.filter(":last") , although doing the best (in performance) of :last , still includes more function calls and is still slower than last() - although it does have its advantages (see @lonesomeday for them).

My recommendation, however, will mainly use last() as opposed to the former.

+1
source

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


All Articles