const _onSuccess = () => {
return {
a: "something",
b: "something else"
}
}
const upload = (successCb) => {
return successCb()
}
const onClick = () => {
return upload(_onSuccess)
}
const onClickCb = () => {
const onSuccess = () => {
return {
a: "something",
b: "something else"
}
}
return upload(onSuccess)
}
window.onClick = onClick
window.onClickCb = onClickCb
onClick()
onClickCb()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
instance method | |
callback |
Test name | Executions per second |
---|---|
instance method | 96347256.0 Ops/sec |
callback | 111812848.0 Ops/sec |
Let's break down what's being tested in the provided JSON.
Benchmark Definition
The benchmark is testing two different approaches to handle an event: using a callback function (onClickCb
) versus using an instance method (onClick
).
In this case, both methods are intended to call an external function upload
, which returns a value. The difference lies in how they pass this value to the upload
function.
Options compared
There are two options being compared:
onClickCb()
onSuccess
is defined within the onClickCb
method.onClickCb
is called, it executes the onSuccess
callback function, which returns a value.upload
function via the callback parameter.onClick()
onClick
is defined and assigned to the window.onClick
property.onClick
is called, it directly returns the result of calling the upload
function with the _onSuccess
variable as a parameter.Pros and Cons
Here are some pros and cons of each approach:
Library and special JS features
There are no libraries explicitly mentioned in the provided JSON. However, it's worth noting that window.onClick
is used as a property name, which might indicate that this code is intended for a browser environment or a specific JavaScript runtime.
No special JavaScript features or syntax are being tested or utilized in this benchmark.
Other alternatives
Some alternative approaches to handling events could include:
addEventListener
or a custom event bus.onClick
) notifies observers (the callback function) of changes.Keep in mind that these alternatives are not directly related to the specific benchmark being tested and would require additional context to be considered viable alternatives.