jQuery event delegation (v37)

Revision 37 of this benchmark created on


Description

Comparing performance of adding .click() to 20 table cells vs using .on() to listen for click events bubbling up the DOM tree.

edit. using an external function

Preparation HTML

<table>
  <tr>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
  </tr>
  <tr>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
  </tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
no .find
$('table td').click(function(){
  $(this).toggleClass('active');
});
ready
no .find external
function toggleFunction(){
  $(this).toggleClass('active');
}

var element = document.getElementsByTagName("td");

for(var i=0, j=element.length; i<j; i++){
   element[i].onclick=toggleFunction;
}
ready
.on
$('table').on('click', 'td', function() {
  $(this).toggleClass('active');
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.