jQuery event delegation
JavaScript performance comparison
Info
Comparing performance of adding .click() to 100 table cells vs using .on() to listen for click events bubbling up the DOM tree.
Preparation code
<div id="parent"></div>
<div id="parent2"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
$('#parent2').on('click', '.clickable', function() {
$(this).toggleClass('active');
});
};
Benchmark.prototype.teardown = function() {
$('.clickable').remove();
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
no delegation |
|
pending… |
with delegation |
|
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:
- Revision 1: published by Scott Kosman
- Revision 2: published
- Revision 3: published by Bertram Simon
- Revision 5: published
- Revision 6: published
- Revision 7: published
- Revision 9: published
- Revision 10: published by Lance Pollard
- Revision 16: published
- Revision 18: published
- Revision 31: published
- Revision 32: published
- Revision 33: published
- Revision 34: published
- Revision 35: published by Aaron Forsander
- Revision 36: published by Aaron Forsander
- Revision 37: published
- Revision 38: published
- Revision 39: published and last updated
- Revision 41: published
- Revision 42: published by Dave Dopson
- Revision 43: published
- Revision 44: published
- Revision 45: published
- Revision 46: published
- Revision 47: published
- Revision 48: published
- Revision 49: published
- Revision 51: published
- Revision 53: published by bibu
- Revision 54: published
- Revision 55: published
- Revision 56: published by iman ghafoori
- Revision 57: published by iman ghafoori
- Revision 58: published by iman ghafoori
- Revision 59: published
- Revision 60: published
- Revision 61: published
- Revision 62: published by Raven
- Revision 63: published
- Revision 64: published
- Revision 65: published
0 comments