jQuery .delegate() on body or document

Benchmark created by Maryk on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="myDiv">
</div>
<script>
  var listArray = [],
      tpl = '   <p class="element" id="check_%i"></p>',
      tplFn = function(value) {
      return tpl.replace(/%i/g, value);
      },
      i;

  // loop and create 1000 entries
  for (i = 0; i <= 1000; i++) {
    listArray.push(tplFn(i));
  }


  $('#myDiv').append(listArray.join(''));
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Delegate with Id
$('#myDiv').delegate('.element', 'click', $.noop);
ready
Delegate with document
$(document).delegate('.element', 'click', $.noop);
ready
Delegate with document.body
$(document.body).delegate('.element', 'click', $.noop);
ready
Delegate with jQuery body
$('body').delegate('.element', 'click', $.noop);
ready

Revisions

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