<!--your preparation HTML code goes here-->
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;
}
for (const row in rows) {
for (const column in columns) {
pargs(row, column)
}
}
for (const row in rows) {
for (const column in columns) {
nargs({ row, column })
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
pargs | |
nargs |
Test name | Executions per second |
---|---|
pargs | 96.1 Ops/sec |
nargs | 96.6 Ops/sec |
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.
Positional Arguments (pargs
):
row
and column
, and computes their combined length using row.length + column.length
. pargs(row, column)
for every combination of row
from rows
and column
from columns
.Named (Destructured) Arguments (nargs
):
row
and column
. It performs the same length calculation as pargs
.pargs
, this function is called within a nested loop but uses nargs({ row, column })
to pass the arguments as an object.The test results show a slight performance difference:
nargs
: 96.59 executions per secondpargs
: 96.10 executions per secondpargs
)Pros:
Cons:
nargs
)Pros:
Cons:
Apart from positional and destructured arguments, there are other ways to handle function parameters, including:
...args
syntax to accept any number of parameters.Overall, the best approach will depend on the specific needs of the application, including factors like code maintainability, performance requirements, and developer familiarity.