let i = 1001;
const results = [];
while (--i) {
window.dispatchEvent(new CustomEvent('custom:test'));
}
function cb() {
console.log('hi')
}
let i = 1001;
const results = [];
while (--i) {
cb();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Custom Event | |
Callback |
Test name | Executions per second |
---|---|
Custom Event | 6204.5 Ops/sec |
Callback | 572.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The provided benchmark measures the performance difference between two approaches: Custom Event
and Callback
. In this specific test case, both methods are used to dispatch an event in a loop, where the number of iterations is fixed at 1001. The goal is to determine which approach yields better performance.
Options compared
There are only two options being compared:
window.dispatchEvent()
function to dispatch a custom event with the name 'custom:test'
. The event object is created using the new CustomEvent()
constructor.cb()
, which logs a message to the console when called.Pros and Cons of each approach
Library usage
Neither of the test cases uses any external libraries, so there are no libraries to describe.
Special JS features or syntax
The CustomEvent
constructor is used, which is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). It allows for creating custom events with additional properties and listeners. The use of this feature suggests that the benchmark aims to measure performance in a real-world scenario where custom events might be used.
Other considerations
The test cases focus on measuring the performance difference between two simple event dispatching methods. However, in a real-world application, other factors like event handling complexity, event listener management, and browser-specific quirks may impact performance.
Alternative approaches
If you were to implement this benchmark from scratch, you might consider using alternative approaches, such as:
addEventListener()
method.Keep in mind that these alternatives would require significant changes to the benchmark test case, but they might provide a more accurate representation of real-world performance scenarios.