开发者

JQUERY Time complexity

开发者 https://www.devze.com 2023-02-28 22:16 出处:网络
I tried the following code for updating the value of an element: first: $(this).parent().prev().children(\':selected\').val();

I tried the following code for updating the value of an element:

first:

$(this).parent().prev().children(':selected').val();

then: I tried with following code:

$(this).parent().prev().children('.pgs').val();//`.pgs` is class of `select` drop down

last:

I also tried with following code:

$(this).parent().prev().find('.pgs').val();
开发者_如何学JAVA

Here, first snippet of code took little more time than the last two snippets. Why did the first snippet show such behavior? How do I find out more about jQuery operations time complexity?


:selected is not a CSS selector the browser understands, it's specificly implemented via Sizzle (the selector engine jQuery's uses), so it goes through a very different code path.

.pgs however is a perfectly valid CSS selector, something that the browser can optimize heavily, since jQuery can use native cSS selection methods, for instance querySelector() and querySelectorAll().

As for more research here, I don't have a great resource for you. However, the general rule is if it's a jQuery selector but not a valid CSS selector it's going to be slower, since it can't take advantage of several native code paths in the browser.

0

精彩评论

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