jquery选择器性能测试

JavaScript performance comparison

Test case created by ehthan

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
< div id = "demo" > < ul > < li > < /li>
 <li></li > < li > < /li>
 <li></li > < /ul>
</div >

Preparation code output

< div id = "demo" > < ul > < li > < /li>
  • < li > < /li>
  • < /ul>

    Test runner

    Warning! For accurate results, please disable Firebug before running the tests. (Why?)

    Java applet disabled.

    Testing in unknown unknown
    Test Ops/sec
    直线选择
    $("#demo li:nth-child(1)")
    pending…
    分段选择
    $("li:nth-child(1)", "#demo")
    pending…
    在元素集合中查找
    $("#demo").find("li:nth-child(1)")
    pending…

    Compare results of other browsers

    Revisions

    You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

    1 comment

    ehthan (revision owner) commented :

    现代浏览器都是从右向左匹配。 $("#demo li:nth-child(1)") 这句将先匹配所有 li元素,在匹配#demo $("#demo").find("li:nth-child(1)") 而这里则先匹配#demo,再从中找匹配li,匹配范围缩短,效率明显提升

    Add a comment