<div id='element'></div>
const element = document.getElementById('element');
const callback = () => { console.log('x') }
element.addEventListener('click', callback);
element.removeEventListener('click', callback);
null
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Adding and removing event listeners | |
Null |
Test name | Executions per second |
---|---|
Adding and removing event listeners | 2759039.5 Ops/sec |
Null | 129327048.0 Ops/sec |
I'll break down the provided JSON benchmark definition, test cases, and latest benchmark result.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark that tests the cost of adding and removing events from an HTML element. Here's what's being tested:
callback
is defined to log the string 'x' to the console.document.getElementById('element')
.callback
function as an argument. This is done using the addEventListener
method.callback
function.The benchmark definition script preparation code and HTML preparation code are provided, allowing users to run the benchmark with a specific setup.
Options compared
In this benchmark, two main options are being compared:
addEventListener
method on the element.removeEventListener
method on the same element.Pros and Cons of those different approaches
Library used
There is no specific library mentioned in the benchmark definition, but document.getElementById
and addEventListener
are part of the standard DOM API.
Special JS feature or syntax
The benchmark uses const
declarations for variable scope, which was introduced in ECMAScript 2015 (ES6). This helps to avoid "variable hoisting" issues.
Other considerations
Alternatives
If you want to create a similar benchmark, consider using:
Keep in mind that the specific details may vary depending on your requirements and goals.