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?
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.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With