最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ":last-of-type" or ".last()" when using JQuery - Stack Overflow

matteradmin7PV0评论

:last-of-type (as of v1.9) vs .last() (as of v1.4): Either method works, but can anyone tell me which is the better method based on efficiency?

Example:

$(".item").last();

$(".item:last-of-type");

".last()" has been around longer (as of v1.4). Does JQuery have ":last-of-type" built-in, or is it just utilizing CSS and piggybacking on the browser's capability? Can anyone confirm this?

It would seem to me that ":last-of-type" would be better when applicable if this is the case.

:last-of-type (as of v1.9) vs .last() (as of v1.4): Either method works, but can anyone tell me which is the better method based on efficiency?

Example:

$(".item").last();

$(".item:last-of-type");

".last()" has been around longer (as of v1.4). Does JQuery have ":last-of-type" built-in, or is it just utilizing CSS and piggybacking on the browser's capability? Can anyone confirm this?

It would seem to me that ":last-of-type" would be better when applicable if this is the case.

Share Improve this question asked Oct 21, 2016 at 22:46 JoePCJoePC 1842 silver badges16 bronze badges 5
  • 1 Why not run some jsperf tests? – j08691 Commented Oct 21, 2016 at 22:48
  • In general, jQuery selectors will use native selectors when possible. – Barmar Commented Oct 21, 2016 at 22:53
  • The selectors perform two different selections, no? – guest271314 Commented Oct 21, 2016 at 23:01
  • 1 They don't do even remotely the same thing, last() just gets the last element in the collection, while :last-of-type queries the DOM and finds all elements that are last of it's type in that parent element, i.e. among siblings with the same tagname – adeneo Commented Oct 21, 2016 at 23:12
  • I do understand the difference and I wasn't saying they do exactly the same thing. I was trying to figure out which one I should use for efficiency. You're right as far as parison. Maybe ":last" (link) rather than ":last-of-type" would have been a better example? – JoePC Commented Oct 21, 2016 at 23:34
Add a ment  | 

1 Answer 1

Reset to default 6

.last() is faster.

Note, the selectors perform different selections

:last-of-type

last-of-type selector

Description: Selects all elements that are the last among siblings of the same element name.

.last()

.last()

Description: Reduce the set of matched elements to the final one in the set.

console.time("lastOfType");
$(".item:last-of-type");
console.timeEnd("lastOfType");

console.time("last");
$(".item").last();
console.timeEnd("last");
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>

Post a comment

comment list (0)

  1. No comments so far