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?
-
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
2 Answers
Reset to default 10Time 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