jQuery.click() vs onClick

前端 未结 17 1477
情话喂你
情话喂你 2020-11-21 07:35

I have a huge jQuery application, and I\'m using the below two methods for click events.

First method

HTML

17条回答
  •  清酒与你
    2020-11-21 08:06

    Performance

    There are already many good answers here however, authors sometimes mention about performance but actually nobody investigate it yet - so I will focus on this aspect here. Today I perform test on Chrome 83.0, Safari 13.1 and Firefox 77.0 for solutions mention in question and additionally few alternative solutions (some of them was mention in other answers).

    Results

    I compare here solutions A-H because they operate on elements id. I also show results for solutions which use class (I,J,K) as reference.

    • solution based on html-inline handler binding (B) is fast and fastest for Chrome and fastest for small number of elements
    • solutions based on getElementById (C,D) are fast, and for big number of elements fastest on Safari and Firefox
    • referenced solutions I,J based are fastest for big num of elements so It is worth to consider use class instead id approach in this case
    • solution based on jQuery.click (A) is slowest

    Details

    Actually It was not easy to design performance test for this question. I notice that for all tested solutions, performance of triggering events for 10K div-s was fast and manually I was not able to detect any differences between them (you can run below snippet to check it yourself). So I focus on measure execution time of generate html and bind event handlers for two cases

    • 10 divs - you can run test HERE
    • 1000 divs - you can run test HERE

    // https://stackoverflow.com/questions/12627443/jquery-click-vs-onclick
    let a= [...Array(10000)];
    
    function clean() { test.innerHTML = ''; console.clear() }
    
    function divFunction(el) {
      console.log(`clicked on: ${el.id}`);
    }
    
    function initA() {
      test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> $(`#myDiv${i}`).click(e=> divFunction(e.target))); } function initB() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; } function initC() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> document.getElementById(`myDiv${i}`).onclick = e=> divFunction(e.target) ); } function initD() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> document.getElementById(`myDiv${i}`).addEventListener('click', e=> divFunction(e.target) )); } function initE() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> document.querySelector(`#myDiv${i}`).onclick = e=> divFunction(e.target) ); } function initF() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> document.querySelector(`#myDiv${i}`).addEventListener('click', e=> divFunction(e.target) )); } function initG() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> window[`myDiv${i}`].onclick = e=> divFunction(e.target) ); } function initH() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; a.map((x,i)=> window[`myDiv${i}`].addEventListener('click',e=> divFunction(e.target))); } function initI() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; [...document.querySelectorAll(`.box`)].map(el => el.onclick = e=> divFunction(e.target)); } function initJ() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; [...document.querySelectorAll(`.box`)].map(el => el.addEventListener('click', e=> divFunction(e.target))); } function initK() { test.innerHTML = a.map((x,i)=> `
    ${i}
    `).join``; $(`.box`).click(e=> divFunction(e.target)); } function measure(f) { console.time("measure "+f.name); f(); console.timeEnd("measure "+f.name) }
    #test {
      display: flex;
      flex-wrap: wrap;
    }
    
    .box {
      margin: 1px;
      height: 10px;
      background: red;
      font-size: 10px;
      cursor: pointer;
    }
    
    
    This snippet only presents used solutions. Click to solution button and then click on any red box to trigger its handler

    Here is example test for Chrome

提交回复
热议问题