最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - :gt(0) vs :not(:first) vs .slice(1) - Stack Overflow

programmeradmin0浏览0评论

Before this I've always used gt selector to select all elements except first. Now I found solution that seems more elegant to me. It's to use :not(:first) instead of :gt(0).
Is there any differences in performance of this selectors and which one do you suggest to use?

EDIT: As mentioned Felix King, .slice(1) is another option to select all elements except first. So which is faster?

Before this I've always used gt selector to select all elements except first. Now I found solution that seems more elegant to me. It's to use :not(:first) instead of :gt(0).
Is there any differences in performance of this selectors and which one do you suggest to use?

EDIT: As mentioned Felix King, .slice(1) is another option to select all elements except first. So which is faster?

Share edited Feb 27, 2012 at 12:58 Chuck Norris asked Feb 27, 2012 at 12:42 Chuck NorrisChuck Norris 15.2k15 gold badges95 silver badges127 bronze badges 2
  • 8 You can also try .slice(1) instead of a custom selector. – Felix Kling Commented Feb 27, 2012 at 12:45
  • They are both slow, method suggested by @FelixKling should be the fastest one. – Artem Koshelev Commented Feb 27, 2012 at 12:48
Add a ment  | 

2 Answers 2

Reset to default 10

Time for a bit of profiling! Given a page that’s empty apart from ten <span>s cached into a variable called spans and 10,000 iterations I get 824ms for spans.filter(':gt(0)') and 1276ms for spans.not(':first').

Figure achieved using console.time() and console.timeEnd() in Firefox 11.

Considering I had to do 10k iterations to hit the 1sec mark I’d suggest it doesn’t matter?

Wrote a jsperf test for this:

http://jsperf./select-all-but-first-42

Turns out the slice method is fastest!

There's another test on jsperf for the same requirement:

http://jsperf./select-all-but-first

发布评论

评论列表(0)

  1. 暂无评论