search

Home  >  Q&A  >  body text

Which is more efficient, $("#content .abc") or $("#content").find(".abc")?

Which is more efficient,

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

天蓬老师天蓬老师2736 days ago964

reply all(4)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-26 10:52:26

    $("#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...

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 10:52:26

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

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-26 10:52:26

    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...

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-26 10:52:26

    The second one is faster

    reply
    0
  • Cancelreply