Jquery element+class selector performance

后端 未结 4 937
萌比男神i
萌比男神i 2020-12-12 18:09

I was hoping $(\'#childDiv2 .txtClass\') or $(\'#childDiv2 input.txtClass\') perform better when selecting

4条回答
  •  死守一世寂寞
    2020-12-12 19:06

    Looks like it also depends on the density of the elements with the class among the elements of the type.

    I ran the tests with Google Chrome Version 30.0.1599.69 using JQuery 1.10.1. Feel free to try it on another browser and/or using another JQuery version.

    I tried to run the following tests:

    1. Sparse (10% of the div's have the class) link to the test on jsbin

    2. Dense (90% of the div's have the class) link to the test on jsbin

    Looks like in the Dense case div.class wins, but in the Sparse case .class wins.

提交回复
热议问题