Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs apply
(version: 0)
Comparing performance of:
a vs b vs c vs d
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function test() { console.log(arguments[arguments.length - 1]); } var using = (new Array(2)).fill(null).map((e, i) => (i)); function a(...args) { test(...args); } function b() { test(...arguments); } function c(...args) { test.apply(null, args); } function d() { test.apply(null, arguments); }
Tests:
a
a(1,2)
b
b(1,2)
c
c(1,2)
d
d(1,2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
a
b
c
d
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):
Let's break down the benchmark and explain what is being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark compares four functions: `a`, `b`, `c`, and `d`. All four functions call a shared test function `test` with either the spread operator (`...args`) or `arguments[arguments.length - 1]`. **Functionality of each function** - **`a(...args)`**: Calls `test` using the spread operator. - **`b()```**: Calls `test` by passing the arguments as separate function parameters. - **`c(...args)`**: Calls `test` using the `apply()` method with `null` as the first argument (which is equivalent to calling it without any explicit receiver). - **`d()```**: Calls `test` using the `apply()` method, similar to `c`, but with passing the arguments directly. **Spread Operator (`...args`) vs. `arguments[arguments.length - 1]`** The spread operator allows you to expand an array or object into separate function parameters. This can be useful for creating reusable functions that accept any number of arguments. On the other hand, using `arguments[arguments.length - 1]` is a more traditional way to pass variable-length argument lists in JavaScript. The `arguments` object contains all passed arguments as individual properties. **Pros and Cons** - **Spread Operator (`...args`):** * Pros: + More readable code (especially when dealing with large number of arguments). + Can be used with arrow functions. * Cons: + Performance might be slightly worse due to the overhead of creating a new array. - **Using `arguments[arguments.length - 1]`:** * Pros: + Performance is generally better, as it directly accesses an existing property on the function call object. * Cons: + Less readable code, especially for functions with many arguments. **Other Considerations** - The benchmark uses Chrome 116, which might have specific optimizations or quirks. Results may vary across other browsers and platforms. - The `test` function is defined to log the last argument passed to it, but its actual purpose is not relevant to the comparison being made between the four functions. - There are no explicit checks for null or undefined arguments in any of the test cases. **Alternatives** There aren't many alternatives to the approach used here. The main consideration would be whether you prefer a more readable code style (using spread operator) or a performance-oriented one (using `arguments[arguments.length - 1]`). However, if readability is paramount, you might consider using libraries like Lodash's `rest` function or the `rest parameter` feature in modern JavaScript versions, which makes it easy to extract all arguments from a function call and make them available as separate variables. If performance is critical, you could look into more complex techniques such as memoization or caching to reduce unnecessary calls to the `test` function.
Related benchmarks:
Spread operator vs apply
Spread operator vs apply - 2
Spread operator vs apply vs apply undefined
Spread operator vs apply vs apply undefined. length 6
Spread operator vs apply - realistic
Comments
Confirm delete:
Do you really want to delete benchmark?