Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:gt(0) vs :not(:first) vs .slice(1)

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?

like image 586
Chuck Norris Avatar asked Feb 27 '12 12:02

Chuck Norris


2 Answers

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?

like image 119
Robin Whittleton Avatar answered Nov 13 '22 09:11

Robin Whittleton


Wrote a jsperf test for this:

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

Turns out the slice method is fastest!

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

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

like image 21
skyronic Avatar answered Nov 13 '22 07:11

skyronic