<button>Test Click</button>
<div>Show result: <span id="result"></span>
</div>
var button = document.querySelector("button");
button.addEventListener("click", function(){
document.getElementById("result").innerText = "da click";
});
var button = document.querySelector("button");
button.onclick = function(){
document.getElementById("result").innerText = "da click";
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
addEventListener | |
onClick |
Test name | Executions per second |
---|---|
addEventListener | 3436.5 Ops/sec |
onClick | 2656.9 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a simple demo application with two test cases: addEventListener
and onClick
.
Script Preparation Code
The script preparation code is empty, which means that the user has not provided any custom initialization code for their benchmark.
Html Preparation Code
The HTML preparation code defines a basic HTML structure with a button element and a div
element to display the result. The button element will trigger one of two events: addEventListener
or onClick
.
Test Cases
There are two test cases:
addEventListener
: This test case benchmarks the performance of adding an event listener to an element using the addEventListener
method.var button = document.querySelector("button");
button.addEventListener("click", function() {
document.getElementById("result").innerText = "da click";
});
Pros and Cons:
addEventListener
method allows for more flexibility and control over event handling, as it enables the use of custom event handlers.onClick
: This test case benchmarks the performance of setting an onclick
attribute on an element.var button = document.querySelector("button");
button.onclick = function() {
document.getElementById("result").innerText = "da click";
};
Pros and Cons:
onclick
attribute is simpler to set up and manage, especially for simple use cases where there are few events being handled.addEventListener
method.Library and Special Features
There is no library used in these test cases. However, it's worth noting that the benchmark uses a standard JavaScript API (Document Object Model - DOM) for accessing HTML elements.
Other Considerations
When writing benchmarks like this one, it's essential to consider factors such as:
Alternatives
Some alternatives for writing benchmarks include:
Keep in mind that the choice of alternative will depend on specific use cases and requirements.