Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance differences between using ":not" and ".not()" selectors?

Are there any speed/efficiency differences between the following two lines.

$("table td:not(:first-child)") 

and

$("table td").not(":first-child") 

I would think that the first would be better since it is removes objects, but is there an actual difference and is it substantial.

Thanks

like image 364
kwelch Avatar asked Jan 13 '12 04:01

kwelch


People also ask

What is the fastest way to select elements by using CSS selectors?

popupbutton is the fastest.

What is the main difference between selectors and filters?

jQuery selector selects all elements based on the elements name given by you. jQuery filter( ) adds further detail to the selected elements by specifying the criteria of selection.

Which selector is faster ID or class?

ID is absolutely the fastest. Part of the reason is that ID is supposed to be unique, so the API stops searching after the ID is found in the DOM. If you must use a class or attribute selector, you can improve performance by specifying the optional context parameter.


1 Answers

As you can see from the jsperf test, :not is on average about twice as fast. Overall though this performance will likely be a very small part of your overall execution time.

The jquery docs state:

The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice.

So really it's up to you to decide if the fractions of a second you gain outweigh the readability.

like image 106
James Montagne Avatar answered Sep 29 '22 21:09

James Montagne