Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread operator vs apply vs normal
(version: 0)
Compare the differing ways you can call a function with arbitrary arguments dynamically
Comparing performance of:
spread vs apply vs normal
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function test(a , b , c , d , e) { return a + b + c + d + e } var using = (new Array(5)).fill(null).map((e, i) => (i));
Tests:
spread
test(...using);
apply
test.apply(null, using)
normal
test(using[0] , using[1] , using[2] , using[3] , using[4])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread
apply
normal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
12584240.0 Ops/sec
apply
12446070.0 Ops/sec
normal
5325731.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Overview** The benchmark compares three ways to call a function `test` with five dynamic arguments: using the spread operator (`...`), the `apply()` method, and calling each argument individually. **Test Cases** 1. **Spread Operator**: The test calls `test(...using);`, where `using` is an array of length 5 created using the spread operator. 2. **Apply Method**: The test calls `test.apply(null, using)`, where `using` is passed as the first argument to `apply()`. 3. **Normal Call**: The test calls `test(using[0] , using[1] , using[2] , using[3] , using[4])`, where each element of `using` is called individually. **Libraries and Features** None. **Options Compared** The benchmark compares three approaches to calling a function with dynamic arguments: * **Spread Operator (`...`)**: This syntax creates an array from the given arguments. The test passes this array as a single argument to the function. * **Apply Method**: This method applies the given arguments to the function, effectively "applying" them in the order they are passed. * **Normal Call (individual elements)**: Each element of `using` is called individually and passed as separate arguments to the function. **Pros and Cons** 1. **Spread Operator (`...`)** * Pros: + Can be more concise and expressive. + Allows for easy creation of arrays from individual elements. * Cons: + May be less efficient than other approaches, especially for large numbers of arguments. 2. **Apply Method** * Pros: + Allows for fine-grained control over the order in which arguments are applied. + Can be more flexible than normal function calls. * Cons: + Requires passing `this` and the array of arguments, which can be cumbersome. 3. **Normal Call (individual elements)** * Pros: + Straightforward and easy to understand. + Does not require additional libraries or syntax. * Cons: + Can be verbose and less concise than other approaches. **Benchmark Results** The latest benchmark result shows the execution speed for each test case: * **Spread Operator (`...`)**: 12,584,240 executions per second * **Apply Method**: 12,446,070 executions per second * **Normal Call (individual elements)**: 5,325,731 executions per second This suggests that the spread operator approach is the fastest, followed by `apply()`, and then normal function calls. **Alternatives** Some alternative approaches to calling a function with dynamic arguments include: 1. Using `Function.prototype.call()` or `Function.prototype.apply()` (similar to the apply method). 2. Using `bind()` to create a new function that can be called with the desired arguments. 3. Using arrow functions and destructuring to create concise and expressive function calls. However, these approaches are not directly related to the benchmark being discussed, which focuses on comparing three specific methods: spread operator, apply method, and normal call (individual elements).
Related benchmarks:
Spread operator vs apply
Spread operator vs apply - 2
Spread operator vs apply vs apply undefined
Spread operator vs apply - realistic
Comments
Confirm delete:
Do you really want to delete benchmark?