Run details:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0
Firefox 61
Ubuntu
Desktop
6 years ago
Test name Executions per second
.each loop and single on-assignement 233.5 Ops/sec
no loop and .on-assignement on list 1012.6 Ops/sec
assign event for each item 230.0 Ops/sec
HTML Preparation code:
x
 
1
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
2
3
<style>
4
  #container { visibility: hidden; }
5
</style>
6
7
<div id="container">
8
</div>
Script Preparation code:
 
var container = $('#container')
var itemCount = 500;
for (var i = 0; i < itemCount; i++) {
  container.append('<div>Item ' + i + '</div>');
}
var items = container.children();
Tests:
  • .each loop and single on-assignement

     
    items.each(function(index,item) {
      var $item = $(item);
      $item.on('click', function() { console.log('Item clicked'); });
    });
  • no loop and .on-assignement on list

     
    items.on('click', function() { console.log('Item clicked'); });
  • assign event for each item

     
    items.click(function() { console.log('Item clicked'); });