well actually
well actually

Reputation: 12400

Jquery: select.children[index] faster than nth-child selector?

In jquery, say I have an html selector select. Which is faster to get the ith element from the selector? If one is faster, how much faster? By a little or a lot?

select.children[i]

Or

$j('*:nth-child(' + i + ')', select)

Upvotes: 1

Views: 5676

Answers (1)

aziz punjani
aziz punjani

Reputation: 25776

select.children[i] is way faster. This is because it doesn't have the overhead of jQuery. Here's a jsPerf, where you can see the results.

For the first test i used

var select = document.getElementById('select'); 
var option = select.children[2]; 

And the second

var select = document.getElementById('select'); 
var option = $('*:nth-child(' + 2 + ')', select); 

Upvotes: 2

Related Questions