jQuery click event test

JavaScript performance comparison

Test case created by macem

Info

As You see please don't use jQuery .live() method it is very slow, use .delegate() instead.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test">
<p>Don't click: <a href="#test">click</a></p>
</div>

<div id="test1">
<p>Don't click: <a href="#test">click</a></p>
</div>

<div id="test2">
<p>Don't click: <a href="#test">click</a></p>
</div>

<div id="test3">
<p>Don't click: <a href="#test">click</a></p>
</div>
<script>
  $('#test a').click (function(e) {
   return false;
  });
 
  $('#test1 a').live ('click', function(e) {
   return false;
  });
 
  $('#test2').click (function(e) {
   switch (e.target.tagName) {
    case 'A': return false; break;
   }
  });
 
  $('#test3').delegate ('a', 'click', function(e) {
   return false;
  });
</script>

Preparation code output

Don't click: click

Don't click: click

Don't click: click

Don't click: click

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
simple click
$('#test a').click();
pending…
live click
$('#test1 a').click();
pending…
root click
$('#test2 a').click();
pending…
delegate click
$('#test3 a').click();
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