Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Explicit call vs apply
(version: 0)
Comparing performance of:
With 0 parameter vs With 1 parameter vs With 2 parameters vs With 3 parameters vs With 4 parameters vs With 5 parameters
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function wrap(fn) { // fast path if arity < 4, slow path if not switch (fn.length) { case 0: return function() { return fn.call(this) } case 1: return function() { return fn.call(this, this) } case 2: return function(a1) { return fn.call(this, this, a1) } case 3: return function(a1, a2) { return fn.call(this, this, a1, a2) } case 4: return function(a1, a2, a3) { return fn.call(this, this, a1, a2, a3) } default: return function() { const args = [this] for (let i = 0, len = arguments.length; i < len; i++) { args[i + 1] = arguments[i] } return fn.apply(this, args) } } } var f0 = wrap(function() { console.log(Date.now()) }) var f1 = wrap(function(a) { console.log(Date.now(), a) }) var f2 = wrap(function(a, b) { console.log(Date.now(), a, b) }) var f3 = wrap(function(a, b, c) { console.log(Date.now(), a, b, c) }) var f4 = wrap(function(a, b, c, d) { console.log(Date.now(), a, b, c, d) }) var f5 = wrap(function(a, b, c, d, e) { console.log(Date.now(), a, b, c, d, e) })
Tests:
With 0 parameter
f0()
With 1 parameter
f1(Date.now)
With 2 parameters
f2(Date.now, Date.now + 1)
With 3 parameters
f3(Date.now, Date.now + 1, Date.now + 2)
With 4 parameters
f4(Date.now, Date.now + 1, Date.now + 2, Date.now + 3)
With 5 parameters
f5(Date.now, Date.now + 1, Date.now + 2, Date.now + 3, Date.now + 4)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
With 0 parameter
With 1 parameter
With 2 parameters
With 3 parameters
With 4 parameters
With 5 parameters
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of different approaches to calling functions with varying numbers of arguments in JavaScript. The benchmark uses a custom function `wrap` that wraps around the original function and determines which approach is faster based on the number of arguments. **Test Cases** There are six test cases: 1. **With 0 parameter**: Calls the wrapped function without any arguments. 2. **With 1 parameter**: Calls the wrapped function with one argument, `Date.now`. 3. **With 2 parameters**: Calls the wrapped function with two arguments, `Date.now` and `Date.now + 1`. 4. **With 3 parameters**: Calls the wrapped function with three arguments, `Date.now`, `Date.now + 1`, and `Date.now + 2`. 5. **With 4 parameters**: Calls the wrapped function with four arguments, `Date.now`, `Date.now + 1`, `Date.now + 2`, and `Date.now + 3`. 6. **With 5 parameters**: Calls the wrapped function with five arguments, `Date.now`, `Date.now + 1`, `Date.now + 2`, `Date.now + 3`, and `Date.now + 4`. **Approaches Compared** The benchmark compares two approaches: 1. **Fast Path**: For functions with 0-4 arguments, the benchmark uses a fast path that checks the length of the function's argument list to determine which approach is faster. 2. **Slow Path**: For functions with more than 4 arguments, the benchmark falls back to a slow path that uses `fn.apply(this, args)` to call the function. **Pros and Cons** * **Fast Path**: + Pros: Faster for most use cases, as it avoids unnecessary type checking. + Cons: May be slower for functions with more than 4 arguments due to the overhead of the fast path's conditional checks. * **Slow Path**: + Pros: Guarantees performance for all function lengths, but may be slower due to the use of `fn.apply`. + Cons: May incur unnecessary type checking and overhead. **Library and Special Features** The benchmark uses no external libraries. However, it relies on JavaScript's built-in `Date` object to generate argument values for each test case. No special features are required to run this benchmark. **Interpretation of Results** The results show the execution speed of each approach for each test case, measured in executions per second (EPS). The fastest approach is usually the fast path, but there may be exceptions for functions with more than 4 arguments.
Related benchmarks:
Explicit call vs apply
Explicit call vs apply
Spread operator vs apply
converting arguments to Array
Comments
Confirm delete:
Do you really want to delete benchmark?