Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Event listener anonymous function vs named inline function vs external function
(version: 0)
This test compares performance of addEventListener(), when it's passed anonymous function, named inline function and function declared beforehand.
Comparing performance of:
Anonymous function vs Named function as parameter vs External function
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="element"></div>
Script Preparation code:
var element = document.getElementById('element'); var iterations = 250; var dummyString = 'Hello';
Tests:
Anonymous function
for (var i = 0; i < iterations; i++) { element.addEventListener('click', function() { element.textContent = dummyString; }); }
Named function as parameter
for (var i = 0; i < iterations; i++) { element.addEventListener('click', function setText() { element.textContent = dummyString; }); }
External function
function setText() { 'use strict'; element.textContent = dummyString; } for (var i = 0; i < iterations; i++) { element.addEventListener('click', setText); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Anonymous function
Named function as parameter
External function
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):
Measuring the performance of different approaches to adding event listeners to an HTML element is crucial in understanding how JavaScript engines optimize and execute code. **What is being tested?** The test compares three approaches: 1. **Anonymous function**: Passing an anonymous function as a parameter to `addEventListener()`. 2. **Named inline function**: Passing a named, inline function as a parameter to `addEventListener()`. 3. **External function**: Declaring a separate function outside the script and passing its reference as a parameter to `addEventListener()`. **Options compared:** * Anonymous functions vs named inline functions vs external functions. * How do these approaches impact performance? **Pros and Cons of each approach:** 1. **Anonymous function**: * Pros: Simple, concise code, no overhead of declaring separate functions. * Cons: May be slower due to the need for the JavaScript engine to parse and execute the anonymous function on every iteration. 2. **Named inline function**: * Pros: Faster execution compared to anonymous functions since it avoids parsing overhead, but still has some overhead due to declaring a named function. * Cons: More code than needed for simple cases, which can lead to unnecessary complexity. 3. **External function**: * Pros: Can be faster and more efficient because the JavaScript engine can cache and reuse the external function's memory address, reducing the need for parsing on every iteration. * Cons: Requires declaring a separate function, which may add overhead due to the added code. **Library usage:** There is no library explicitly mentioned in the provided benchmark definition or test cases. However, some libraries like jQuery might be used in real-world scenarios with event listeners. **Special JavaScript features/syntax:** The test does not utilize any special JavaScript features or syntax. **Benchmark preparation code and HTML:** * The script preparation code sets up a variable `element` referencing an HTML element with the id "element", defines a variable `iterations`, and a dummy string `dummyString`. * The HTML preparation code creates a basic HTML structure for the test, including an `<div>` element with the id "element". **Other alternatives:** 1. **Event listener factory functions**: Some browsers have implemented event listener factory functions (e.g., Safari) which can be faster than traditional event listeners. 2. **Object-oriented programming approaches**: Using objects to encapsulate event handlers might lead to better performance and maintainability. 3. **Use of `addEventListener` with a single event type**: Passing only the event type instead of an entire object might reduce overhead. These alternatives are not compared in this specific benchmark, but they can be explored as part of further testing or optimization efforts.
Related benchmarks:
Event listener anonymous function vs named inline function vs external function
Event Handler Inline Anonymous vs Named
addEventListener() vs jQuery.on() with click
Cost of adding and removing events
Comments
Confirm delete:
Do you really want to delete benchmark?