Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using :even or filter(":even") in jQuery

I am reading the jQuery API, about even selector, jQuery recommend we select a list of elements use pure CSS Selector and then use filter(":even") for better performance. But I think jQuery optimized for their :even selector too. When I used any selector, it gave me the same result with the same time. Will it have different only when we have more than 1 million of elements? Could someone please explain how the :even selector work and why use filter(":even") is better?

Thanks, Tho Vo

like image 785
Raphaël VO Avatar asked Nov 17 '25 10:11

Raphaël VO


1 Answers

Why not test it? http://jsperf.com/even-selector

If you're looking for something fast, use :nth-child(even), as it's a native selector and can be fed into document.querySelectorAll. :even is a jQuery-specific pseudo-selector and forces jQuery to traverse the DOM using JavaScript, which will almost always be slower than document.querySelector.

like image 194
Blender Avatar answered Nov 20 '25 00:11

Blender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!