<div id="parent">
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
<div>a</div> <div>b</div> <div>c</div> <div>e</div> <div>f</div> <div>g</div> <div>h</div> <div>i</div> <div>j</div> <div>k</div>
</div>
var parent = document.getElementById("parent");
var text ="";
parent.addEventListener("click", (e)=>{
text = e.target.textContent;
});
for(var children of parent.children){
children.addEventListener("click",(e)=>{
text = e.target.textContent;
})
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Parent listener | |
Child listeners |
Test name | Executions per second |
---|---|
Parent listener | 2401.5 Ops/sec |
Child listeners | 45.9 Ops/sec |
Let's break down the benchmark and its various components.
Benchmark Definition
The benchmark is comparing two approaches to attach event listeners:
addEventListener
.for
loop and addEventListener
.Options being compared
In this case, we're only comparing two options.
Pros and Cons of each approach
Library
There is no explicit library mentioned in the benchmark definition. However, it's likely that the addEventListener
method is used, which is a part of the HTML5 specification and is supported by most modern browsers.
Special JS feature/syntax
None are explicitly mentioned in this benchmark. The code uses standard JavaScript syntax, including addEventListener
, forEach
, and template literals.
Other alternatives
If we were to consider alternative approaches:
event.target
property to determine which child element was clicked.The current benchmark provides a simple and straightforward comparison between two approaches. Adding additional alternatives would require more complexity and potentially introduce new variables that could affect the outcome of the benchmark.