开发者

Is one of these syntaxes works faster or both equals?

开发者 https://www.devze.com 2023-03-27 04:35 出处:网络
I heard, that second syntax is much faster. Is it right? $(\'div *\') or $(\'div\').find(\'*\') Or both take equal time?

I heard, that second syntax is much faster. Is it right?

$('div *')

or

$('div').find('*')

Or both take equal time?

EDIT:

Ok, downvoters, lets see t开发者_StackOverflow中文版his TEST (thanks to @AlienWebguy). Can anybody explain?


Exact results may depend upon the page HTML and specific selector query, but this jsperf says that the $('div').find('*') is way, way slower than $('div *') in the HTML case I picked.

For a question like this to be meaningful, you have to specify an exact selector and a body of HTML that you're going to run it against.

Now that you've added a test that uses some specific HTML (but a different selector), I thought I'd run your HTML from that test against the actual selectors you asked about in a test that actually tests what you asked about. In Chrome 13, I find the $('div *') selector to be 9 times faster than $('div').find('*') when run against the HTML in your test. You can see the jsperf here.


in Chrome 10, i don't get the same results as jfriend00.

When i try the test given by DotNET Ninja, $('#div').find('*') is 61% faster than $('#div *'). As far as i understand jquery, it is faster because $('#div *') is interpreted into $('#div').find('*') so this takes longer.

But jquery selectors have been optimized by jquery developpers and you may not get the same results for each selector combination.

0

精彩评论

暂无评论...
验证码 换一张
取 消