Most efficient way to find elements in jQuery

后端 未结 8 1553
执念已碎
执念已碎 2020-12-16 18:19

If I have a CSS class which I only ever apply to form elements, eg:

Which of these two jQuery select

8条回答
  •  再見小時候
    2020-12-16 18:39

    enobrev, Interesting. I just ran your example but using jquery 1.3 beta 2 here

    results.... (1.2.6 speed in brackets)

    // With 100 non-form elements and Context:
    $('.myForm', '#someContainer') : 4063ms (3707ms)
    $('form.myForm', '#someContainer') : 6045ms (4644ms)
    
    // With 100 non-form elements: 
    $('.myForm') : 3954ms (25086ms)
    $('form.myForm') : 8802ms (4057ms)
    
    // Without any other elements with Context: 
    $('.myForm', '#someContainer') : 4137ms (3594ms)
    $('form.myForm', '#someContainer') : 6627ms (4341ms)
    
    // Without any other elements: 
    $('.myForm') : 4278ms (7303ms) 
    $('form.myForm') : 8353ms (4033ms)
    

提交回复
热议问题