Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
delete 4
(version: 5)
delete 4
Comparing performance of:
addEventListener/removeEventListener vs function addEvent/function removeEvent vs function addEvent/function removeEvent ver. MULTI vs jQuery
Created:
9 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="testElement">Test</div> <div id="testElement2">Test 2</div>
Script Preparation code:
var API = API || {}; API.addEvent=function(elem,eventType,fn){ if(elem.addEventListener) elem.addEventListener(eventType,fn,false); else if(elem.attachEvent) elem.attachEvent('on'+eventType,fn); else elem['on'+eventType]=fn; }; API.removeEvent=function(elem,eventType,fn){ if(elem.removeEventListener) elem.removeEventListener(eventType,fn,false); else if(elem.detachEvent) elem.detachEvent('on'+eventType,fn); else elem['on'+eventType]=null; }; API.addEventMULTI=function(o,t,f){ if(!o.length){ if(o.addEventListener) o.addEventListener(t,f,false); else if(o.attachEvent) o.attachEvent('on'+t,f); else o['on'+t]=f; }else{ for(var i=0,len=o.length;i<len;i++){ if(o[i].addEventListener) o[i].addEventListener(t,f,false); else if(o[i].attachEvent) o[i].attachEvent('on'+t,f); else o[i]['on'+t]=f; } } }; API.removeEventMULTI=function(o,t,f){ if(!o.length){ if(o.removeEventListener) o.removeEventListener(t,f,false); else if(o.detachEvent) o.detachEvent('on'+t,f); else o['on'+t]=null; }else{ for(var i=0,len=o.length;i<len;i++){ if(o[i].removeEventListener) o[i].removeEventListener(t,f,false); else if(o[i].detachEvent) o[i].detachEvent('on'+t,f); else o[i]['on'+t]=null; } } }; var elem = document.getElementById('testElement'), elem2 = document.getElementById('testElement2'), elements = document.getElementsByTagName('div'); function test(elem){ elem.innerHTML = "Hello World!"; }
Tests:
addEventListener/removeEventListener
elem.addEventListener('click',test,false); elem.removeEventListener('click',test,false); elem2.addEventListener('click',test,false); elem2.removeEventListener('click',test,false);
function addEvent/function removeEvent
API.addEvent(elem,'click',test); API.removeEvent(elem,'click',test); API.addEvent(elem2,'click',test); API.removeEvent(elem2,'click',test);
function addEvent/function removeEvent ver. MULTI
API.addEventMULTI(elements,'click',test); API.removeEventMULTI(elements,'click',test);
jQuery
$('#testElement').on('click',test); $('#testElement').off('click',test); $('#testElement2').on('click',test); $('#testElement2').off('click',test);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
addEventListener/removeEventListener
function addEvent/function removeEvent
function addEvent/function removeEvent ver. MULTI
jQuery
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested. **Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark tests different approaches for adding and removing event listeners to elements in HTML. **Test Cases** There are four individual test cases: 1. **addEventListener/removeEventListener**: This test case checks the performance of directly attaching and detaching event listeners using `addEventListener` and `removeEventListener`. 2. **function addEvent/function removeEvent**: This test case tests the performance of using a custom API (`API`) to add and remove event listeners. 3. **function addEvent/function removeEvent ver. MULTI**: This test case compares the performance of using the custom API (`API`) with multiple elements (in this case, all `div` elements) versus attaching individual listeners to each element separately. 4. **jQuery**: This test case checks the performance of using jQuery's `.on()` and `.off()` methods to add and remove event listeners. **Benchmarked Libraries and Features** * The custom API (`API`) is used in test cases 2 and 3. It provides a set of functions for adding and removing event listeners, including `addEvent`, `removeEvent`, `addEventMULTI`, and `removeEventMULTI`. These functions are designed to work on both modern browsers and older Internet Explorer versions. * jQuery (version not specified) is used in test case 4. **Options Compared** The benchmark compares the performance of four different approaches: 1. Directly attaching and detaching event listeners using `addEventListener` and `removeEventListener`. 2. Using a custom API (`API`) to add and remove event listeners. 3. Attaching multiple event listeners to elements using the custom API's `addEventMULTI` function. 4. Using jQuery's `.on()` and `.off()` methods. **Pros and Cons** Here are some pros and cons of each approach: 1. **Directly attaching and detaching event listeners**: This is a simple and widely supported method, but it can be slower due to the overhead of creating and destroying event listeners. 2. **Custom API (`API`)**: This provides more control over event listener management, as it allows for custom logic and handling of older browsers. However, it may require more code and have a steeper learning curve. 3. **Attaching multiple event listeners**: This approach can be faster than attaching individual listeners, but it may also lead to unexpected behavior if not implemented correctly. 4. **jQuery's `.on()` and `.off()` methods**: These provide a convenient way to manage event listeners, but they may introduce additional overhead due to the jQuery library. **Other Considerations** * Browser compatibility: The custom API (`API`) is designed to work on both modern browsers and older Internet Explorer versions. * Event listener scope: The benchmark does not account for the scope of event listeners (e.g., whether they are globally or locally scoped). **Alternatives** If you want to create a similar benchmark, you can use MeasureThat.net's own API or write your own JavaScript framework with a focus on event listener management. Alternatively, you could create a custom test suite using a testing framework like Jest or Mocha. By analyzing these results, developers can choose the most efficient approach for managing event listeners in their applications, taking into account factors such as performance, browser compatibility, and code complexity.
Related benchmarks:
delete 3
event delegation (with parent identification) vs local handlers
remove els
_performance_comparison: DeepClone vs removing/adding eventlisteners
Comments
Confirm delete:
Do you really want to delete benchmark?