Why jQuery’s delegate Outperforms Direct click Binding: A Speed Test
This article compares two jQuery event‑binding methods—direct .click() and delegated .delegate()—by generating ten thousand list items, measuring execution times, and showing that delegation reduces binding time from about 104 ms to just 2 ms, demonstrating a significant performance advantage.
When binding events to nodes in jQuery, such as click events, common approaches are:
$('x').click(function (){}); $('x').delegate('childnode', 'click', function (){});The delegate method was introduced later, and many articles recommend it; but what advantages does it have over direct binding?
We generated ten thousand <li> elements and bound click events using both methods to measure their execution times.
Code used to generate the list (PHP) and test the two binding methods:
<html>
<body>
<ul>
<?php for($i=0; $i<10000; $i++){?>
<li><?php echo $i; ?></li>
<?php }?>
</ul>
<script src="js/jquery.js"></script>
<script type="text/javascript">
$(function (){
var st=new Date().getTime();
// Method 1
// $('li').click(function (){
// alert(1);
// });
// Method 2
// $("ul").delegate("li","click",function(){ alert(1); })
var st2=new Date().getTime() - st;
alert(st2);
});
</script>
</body>
</html>Results (average of three runs):
Method 1: 104 ms
Method 2: 2 ms
The delegate approach is dramatically faster, reducing binding time by over 50 times.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
