function wrapExplicit(fn) {
return function(a1, a2, a3) {
return fn.call(this, this, a1, a2, a3)
}
}
function wrapApply(fn) {
return function() {
var args = [this]
for (let i = 0, len = arguments.length; i < len; i++) {
args[i + 1] = arguments[i]
}
return fn.apply(this, args)
}
}
function wrapApplyWithArguments(fn) {
return function() {
return fn.apply(this, [this].concat(arguments))
}
}
function fn(thisArg, a1, a2, a3) {
thisArg.result = a1 + a2 + a3
}
var explicitCall = { fn: wrapExplicit(fn) }
var applyCall = { fn: wrapApply(fn) }
var applyWithArgumentsCall = { fn: wrapApplyWithArguments(fn) }
explicitCall.fn(Math.random(), Math.random(), Math.random())
applyCall.fn(Math.random(), Math.random(), Math.random())
applyWithArgumentsCall.fn(Math.random(), Math.random(), Math.random())
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Explicit call | |
Apply call | |
Apply with arguments call |
Test name | Executions per second |
---|---|
Explicit call | 971056.4 Ops/sec |
Apply call | 856130.4 Ops/sec |
Apply with arguments call | 367501.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Definition
The provided JSON represents a benchmark that tests three different approaches to handling small arity function calls in JavaScript: explicit calls, apply calls, and apply with arguments calls.
In this context, "arity" refers to the number of formal parameters a function takes. In the given benchmark, the fn
function takes 3 arguments (a1
, a2
, and a3
). The goal is to compare the performance of three different wrappers around this function:
fn
function with the correct number of arguments.apply()
method to call the original fn
function with the correct number of arguments, passing the first argument as the context (this
) and subsequent arguments in an array....
) to pass all arguments in an array.Options Comparison
The three approaches have different trade-offs:
Cons:
Library Considerations
None of the provided wrappers use external libraries. However, if we were to extend this benchmark to include more complex functions or utility methods from a library like Lodash, we might see differences in performance.
Special JS Features/Syntax
The benchmark makes use of ES6 features:
wrapExplicit
, wrapApply
, and wrapApplyWithArguments
)...
)While not essential to the basic understanding of the benchmark, it's worth noting that these features are relatively recent additions to the JavaScript language.
Alternatives
If you were to rewrite this benchmark using a different approach or technique, some alternatives might include:
fn.bind()
or fncurry()
from libraries like Lodash.JIT
compiler.In conclusion, this benchmark provides a clear comparison of three approaches to handling small arity function calls in JavaScript. Understanding the trade-offs and benefits of each approach can help developers optimize their code for specific use cases and performance requirements.