Which is more efficient, $("#content .abc") or $("#content").find(".abc")?
天蓬老师
天蓬老师 2017-06-26 10:50:26
0
4
936
Which is more efficient,

$(“#content .abc”) or $(“#content”).find(“.abc”)?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(4)
女神的闺蜜爱上我

$("#content").find(".abc") The .find() method will call the browser's native methods (getElementById, getElementByName, getElementByTagName, etc.), so it is faster. Much faster than $("#content .abc")
About jQuery selector efficiency, you can refer to the performance analysis http://blog.csdn.net/cxl44490...

Ty80
console.time('1')
$("#content .child")
console.timeEnd('1')
console.time('2')
$("#content").find('.child')
console.timeEnd('2')
1: 0.908935546875ms
2: 0.147705078125ms
简单测试第二种快点
滿天的星座

The test result is that find is faster
The reason is that the selection order of various selector chains used internally by jQuery is from right to left, so this statement first selects .abc, and then filters out the parent element #content one by one, which results in It's much slower.
At the same time, the .find() method will call the browser's native methods (getElementById, getElementByName, getElementByTagName, etc.), so it is faster.
Comparing the two, naturally find is much faster.

The following is the test: https://jsperf.com/jqueryfind...

滿天的星座

The second one is faster

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template