<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
function test(args) {
let finalRep = '';
for(const item of args) {
finalRep = item.toString();
}
return finalRep;
}
const frep = test('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z');
function test(args) {
let finalRep = '';
for(const item of args) {
finalRep = item.toString();
}
return finalRep;
}
const frep = test(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
variadic | |
array |
Test name | Executions per second |
---|---|
variadic | 10713618.0 Ops/sec |
array | 11909563.0 Ops/sec |
This benchmark titled "variadics vs arrays" tests the performance differences between two ways of passing parameters to functions in JavaScript: using variadic parameters (...args
) versus passing an array. Specifically, it examines whether the variadic syntax is faster or slower than traditional array argument passing when handling a set of string inputs.
Variadic Parameters (...args
):
function test(...args) {
let finalRep = '';
for (const item of args) {
finalRep = item.toString();
}
return finalRep;
}
const frep = test('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
args
array-like structure. It converts each item to a string and keeps overwriting finalRep
with the last string's value.Array as Argument:
function test(args) {
let finalRep = '';
for (const item of args) {
finalRep = item.toString();
}
return finalRep;
}
const frep = test(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
finalRep
similarly to the variadic version.The benchmark results show that:
Pros:
Cons:
Pros:
Cons:
...array
) and object destructuring to create even more flexible APIs.These benchmarks help inform developers not only about performance but also guide them in choosing the right structure based on their specific use case.