jQuery event delegation (v35)

Revision 35 of this benchmark created by Aaron Forsander on


Description

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

Preparation HTML

<div id="parent"></div>
<div id="parent2"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Setup

$('#parent2').on('click', '.clickable', function() {
      $(this).toggleClass('active');
    });

Teardown


    $('.clickable').remove();
  

Test runner

Ready to run.

Testing in
TestOps/sec
no delegation
var $target = $("<div class='clickable'></div>").appendTo('#parent');
$('#parent').on('click', '.clickable', function() {
  $(this).toggleClass('active');
});
$target.click();
ready
with delegation
var $target = $("<div class='clickable'></div>").appendTo('#parent2');
$target.click();
ready

Revisions

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