Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arguments vs rest
(version: 1)
Comparing performance of:
arguments vs rest
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function max1() { return Math.max.apply(1, arguments) } function max2(...a) { return Math.max.apply(1, a) }
Tests:
arguments
for(let i = 300; i--;) { max1(...Array(i).fill(i)) }
rest
for(let i = 300; i--;) { max2(...Array(i).fill(i)) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
arguments
rest
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 141 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
arguments
1384.7 Ops/sec
rest
1377.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark titled "arguments vs rest" compares two different methods for handling variable numbers of function arguments in JavaScript: using the `arguments` object and using the rest parameter syntax (`...`). ### Options Compared: 1. **Using `arguments` Object** (`max1` function): - Definition: This function utilizes the `arguments` object, which is an array-like object available inside all non-arrow functions. It holds all the arguments passed to the function. - Benchmark Code: `Math.max.apply(1, arguments)`: Here, `Math.max()` is called with the `arguments` object, enabling it to find the maximum value among the arguments passed to the function. 2. **Using Rest Parameter Syntax** (`max2` function): - Definition: This function utilizes the rest parameter syntax, where the function definition uses `...a` to collect all remaining arguments into an actual array named `a`. - Benchmark Code: `Math.max.apply(1, a)`: Similar to the first function, but it passes the rest parameter `a` to `Math.max()`. ### Pros and Cons: - **Using `arguments` Object:** - **Pros:** - Supported in all JavaScript environments and function types (including older ones). - No additional code or syntax needed for older JavaScript. - **Cons:** - Does not have the same array methods or properties as a normal array (e.g., `.map()`, `.filter()`). - Can lead to less readable code, especially when manipulating argument values. - **Using Rest Parameter:** - **Pros:** - Makes the function signature more explicit and readable. - Arguments are directly passed as an Array, enabling the use of all array methods seamlessly. - More modern syntax, aligns with ES6+ practices. - **Cons:** - Not supported in older JavaScript environments (pre-ES6), though this is less of a concern today with modern browsers. ### Other Considerations: 1. **Performance**: - In the benchmark results, the `arguments` method executed at approximately 6990.15 executions per second, while the rest parameter method executed at about 6592.25 executions per second. This test shows that utilizing the `arguments` object performed better in this benchmarking scenario. 2. **Cross-Browser Compatibility**: - While both methods will run in modern JavaScript engines, there may be slight performance variations across different browsers or versions. It's generally a good idea to test in the environments where the code will run. 3. **Code Maintainability**: - Choosing between these two methods may ultimately depend on team conventions and the coding style preferred in your application. The rest parameter is usually favored in modern code for better clarity. ### Alternatives: 1. **Array Destructuring**: This approach involves passing an array to a function and destructuring it inside. It's similar to using the rest parameter but allows for more structured arguments. ```javascript function maxFromArray(arr) { return Math.max(...arr); } ``` 2. **Using `apply` with Arrays directly**: If already working with an array, you can call `Math.max.apply(null, arr)`, which achieves similar results without needing to handle arguments directly. Overall, the choice between using the `arguments` object or rest parameters will depend on the specific scenarios of the code being written, consideration for performance, and preference for modern syntax. The benchmark presented provides critical insight into performance differences in using these different approaches in JavaScript.
Related benchmarks:
else vs continue
else vs continue vs short if
Array includes 20190424
Array for vs. some
for vs for of vs for in
pre fill array
For of vs for each
let vs array 2
Set replace
Comments
Confirm delete:
Do you really want to delete benchmark?