Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Rest parameters vs array parameter v2
(version: 1)
Comparing performance of:
Rest vs Array
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const f1 = (a, ...b) => a.concat(b.map(String)); const f2 = (a, b) => a.concat(b.map(String));
Tests:
Rest
const r1 = f1([0], 1, 2, 3, 4, 5);
Array
const r2 = f2([0], [1, 2, 3, 4, 5]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Rest
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Rest
13407240.0 Ops/sec
Array
13731010.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of two different ways to handle function parameters in JavaScript: using **rest parameters** and using **array parameters**. ### Tested Approaches: 1. **Rest Parameters (`f1`)**: This syntax allows a function to accept an indefinite number of arguments as an array. In the benchmark, `f1` is defined as: ```javascript const f1 = (a, ...b) => a.concat(b.map(String)); ``` Here, `a` is the first argument, and `b` is an array containing the rest of the arguments passed to the function. 2. **Array Parameters (`f2`)**: This approach uses a traditional method of passing an array as one of the parameters. In the benchmark, `f2` is defined as: ```javascript const f2 = (a, b) => a.concat(b.map(String)); ``` Here, `a` is a single argument, and `b` is also expected to be an array. ### Pros and Cons of Each Approach: - **Rest Parameters**: - **Pros**: - Allows for a more flexible number of parameters. - Simplifies the handling of arguments since they are automatically bundled into an array. - Can directly work with additional arguments without needing to wrap them in another array. - **Cons**: - Potentially more overhead in terms of performance due to creating an additional array (the rest parameter) in memory. - **Array Parameters**: - **Pros**: - Can be more efficient in terms of performance, particularly when the number of arguments is known or fixed since it directly manipulates the input array. - Uses traditional JavaScript paradigms, which can be familiar to developers. - **Cons**: - Requires the caller to manually construct the array of arguments, which adds complexity if the number of arguments is variable. ### Considerations: - The results show that the array parameter approach (`f2`) executed roughly **13,731,010** times per second, while the rest parameter approach (`f1`) executed about **13,407,240** times per second. This indicates that in this specific benchmarking setup, the array parameter approach was marginally faster. - The choice between these two methods might depend on the context of the function usage: if the function needs to handle a variable number of arguments, rest parameters may be more suitable despite their slight performance cost. - For fixed or known input sizes, the array parameters might be preferred to achieve better performance. ### Alternatives: Apart from rest and array parameters, JavaScript developers may consider: - **Fixed Arguments**: Define functions with a specific number of parameters (e.g., `function myFunc(a, b, c) {}`), which can lead to clearer code when the number of arguments is known, but lacks flexibility. - **Object Parameters**: Using an object to pass multiple parameters (`function myFunc({ a, b, c }) {}`) can enhance readability and avoid parameter order issues, but introduces some overhead in creating the object. - **Variadic Functions**: Similar to rest parameters, but can emulate behavior through invocation using the `arguments` object (applicable in non-arrow functions), though this method has been discouraged in modern JavaScript due to its inherent limitations and lack of clarity. In summary, selecting between rest and array parameters depends on the specific use case, and developers should consider both performance and code clarity when making their decision.
Related benchmarks:
Array concat vs spread
concat vs spread 2
Rest parameters vs array parameter
Rest parameters vs array parameter with empty parameters
Spread vs concat vs forEach array v1001
concat 1 vs multiple
Rest parameters vs array parameter 2
object concat vs spread vs concat
Rest parameters vs array parameter v3
Comments
Confirm delete:
Do you really want to delete benchmark?