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

Before that, I always used the gt selector to select all elements except the first. Now I have found a solution that seems to me more elegant. Use :not(:first) instead of :gt(0) .
Are there any performance differences between these selectors and which ones do you propose to use?

EDIT: As already mentioned, Felix King, .slice(1) is another option to select all items except the first. So what is faster?

+7
source share
2 answers

Time for a little profiling! Given that the page is blank except for ten <span> cached into a variable named 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.

Given that I needed to do 10k iterations to hit the 1 second mark. Id tells you that it doesn't matter?

+10
source

Wrote for this the jsperf test:

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

Turns off the cut method faster!

Another jsperf test for the same requirement:

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

+3
source

All Articles