哪个更快:$("li").last()或$("li:last-child")? [英] Which is faster: $("li").last() or $("li:last-child")?

查看:79
本文介绍了哪个更快:$("li").last()或$("li:last-child")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery中,执行速度更快:$("li").last()$("li:last-child")吗?

In jQuery, which is faster to execute: $("li").last() or $("li:last-child") ?

推荐答案

尽管两者的工作方式不同,但这是我从jsperf获得的结果:

Though both are different in what they do but here is result I got from jsperf:

http://jsperf.com/li-last-vs-li-最后一个孩子

$("li").last()

28,752
±8.59%
fastest

还有

$("li:last-child")

21,930
±4.01%
24% slower

所以$("li").last()更快.

FYI $("li").last()选择绝对的最后一个元素,而$("li:last-child")选择一系列分组元素中的 每个 最后一个元素.这也清楚表明$("li").last()应该更快.

FYI $("li").last() selects the absolute last element whereas $("li:last-child") selects every last element in a series of grouped elements. That also makes it obvious that $("li").last() should be faster which it is.

这篇关于哪个更快:$("li").last()或$("li:last-child")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆