jQuery event delegation

JavaScript performance comparison

Revision 37 of this test case created

Info

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 code

<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="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
no .find
$('table td').click(function(){
  $(this).toggleClass('active');
});
pending…
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;
}
pending…
.on
$('table').on('click', 'td', function() {
  $(this).toggleClass('active');
});
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:

0 comments

Add a comment