Jquery:select.children[index]比第n个子选择器快吗?

Jquery:select.children[index]比第n个子选择器快吗?,jquery,jquery-selectors,css-selectors,Jquery,Jquery Selectors,Css Selectors,在jquery中,假设我有一个html选择器select。从选择器中获取第i个元素的速度哪一个更快?如果一个更快,有多快?是少还是多 select.children[i] 或 select.children[i]要快得多。这是因为它没有jQuery的开销。这里有一个,你可以看到结果 在我使用的第一次测试中 var select = document.getElementById('select'); var option = select.children[2]; 第二个呢 var se

在jquery中,假设我有一个html选择器select。从选择器中获取第i个元素的速度哪一个更快?如果一个更快,有多快?是少还是多

select.children[i]


select.children[i]
要快得多。这是因为它没有jQuery的开销。这里有一个,你可以看到结果

在我使用的第一次测试中

var select = document.getElementById('select'); 
var option = select.children[2]; 
第二个呢

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

我可以建议你去看看吗?它将允许您非常轻松地对JavaScript的性能进行基准测试。那么,为什么不计时呢?即使对两者都使用jQuery,索引方法(select.children()[i])的速度大约是前者的两倍。真的吗?我得到了一些矛盾的结果,请查看更新的jsperf,我添加了一些测试。只需运行perf(Fx 8)和
$…children()[I]
实际上比
第n个child
变体快三倍左右。我的表现只有两次左右:
var select = document.getElementById('select'); 
var option = $('*:nth-child(' + 2 + ')', select);