Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
named vs positional arguments v2
(version: 1)
Comparing performance of:
pargs vs nargs
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const columns = Array.from({ length: 100 }, (_, i) => `column-${i}`) const rows = Array.from({ length: 10_000 }, (_, i) => `row-${i}`) function pargs(row, column) { return row.length + column.length; } function nargs({ row, column }) { return row.length + column.length; }
Tests:
pargs
for (const row in rows) { for (const column in columns) { pargs(row, column) } }
nargs
for (const row in rows) { for (const column in columns) { nargs({ row, column }) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
pargs
nargs
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pargs
96.1 Ops/sec
nargs
96.6 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: positional arguments (`pargs`) and named (or destructured) arguments (`nargs`). The goal is to determine which approach is more efficient in terms of execution speed. ### Benchmarked Functions 1. **Positional Arguments (`pargs`)**: - Definition: This function takes two parameters, `row` and `column`, and computes their combined length using `row.length + column.length`. - Usage in Benchmark: The benchmark runs a nested loop where it calls `pargs(row, column)` for every combination of `row` from `rows` and `column` from `columns`. 2. **Named (Destructured) Arguments (`nargs`)**: - Definition: This function accepts a single object parameter that is destructured into `row` and `column`. It performs the same length calculation as `pargs`. - Usage in Benchmark: Similar to `pargs`, this function is called within a nested loop but uses `nargs({ row, column })` to pass the arguments as an object. ### Performance Results The test results show a slight performance difference: - **`nargs`**: 96.59 executions per second - **`pargs`**: 96.10 executions per second ### Pros and Cons of Each Approach #### Positional Arguments (`pargs`) - **Pros**: - Generally faster in practice because fewer operations are needed to access the parameters. - Simplicity in calling functions when the number of parameters is fixed and known. - **Cons**: - Less readable, especially with multiple parameters; the order of parameters must be remembered. - More error-prone; calling the function with the wrong order can lead to unexpected behaviors. #### Named Arguments / Destructuring (`nargs`) - **Pros**: - Enhanced readability; parameter names are clear and self-documenting. - Flexibility; allows for optional parameters without changing the function signature. - Less error-prone if objects are constructed with clear identifiers. - **Cons**: - Slightly slower because object creation and destructuring incur some overhead. - More complex function signatures can make it harder to understand at a glance for less experienced developers. ### Other Considerations - The choice between these two strategies may depend on the specific application. For performance-sensitive applications, positional arguments might be favored; whereas, for ease of maintenance and clarity, destructured arguments could be preferable. - The size of the input data (i.e., number of rows and columns) can also influence the performance results more dramatically than the choice of argument passing style when scaling up. ### Alternatives Apart from positional and destructured arguments, there are other ways to handle function parameters, including: - **Default Parameters**: Allowing functions to be called with some parameters omitted. - **Rest Parameters**: Using `...args` syntax to accept any number of parameters. - **Object Parameters without Destructuring**: Passing a single object as an argument without destructuring, which retains some clarity but may not be as readable as destructured syntax. Overall, the best approach will depend on the specific needs of the application, including factors like code maintainability, performance requirements, and developer familiarity.
Related benchmarks:
Array creation
flatten
using data set. array vs object
JS array for test
qwe1234123
Comparação entre maps
test another
for, for..of, forEach
named vs positional arguments v1
Comments
Confirm delete:
Do you really want to delete benchmark?