fr = (a, b) => a.concat(b.map(String))
fa = (a, b) => a.concat(b.map(String))
af = [1, 2, 3, 4, 5];
ae = [];
const r = fr([0], 1, 2, 3, 4, 5);
const r = fa([0], [1, 2, 3, 4, 5]);
const r = fr([0]);
const r = fa([0], []);
const r = fa([0], af);
const r = fa([0], ae);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Rest | |
Array | |
Rest Empty | |
Array Empty | |
Array Predefined | |
Array Empty Predefined |
Test name | Executions per second |
---|---|
Rest | 3946815.0 Ops/sec |
Array | 3711217.5 Ops/sec |
Rest Empty | 5080134.5 Ops/sec |
Array Empty | 4930416.5 Ops/sec |
Array Predefined | 3038471.5 Ops/sec |
Array Empty Predefined | 4024380.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The provided JSON defines a benchmark named "Rest parameters vs array parameter with empty parameters". This benchmark tests two different approaches to handling rest parameters (the dots ...
in function arguments) versus using an array as a parameter.
Options Compared
Two options are compared:
fr = (a, ...b) => a.concat(b.map(String))
. This approach uses the rest parameter syntax to capture any additional arguments passed to the function.fa = (a, b) => a.concat(b.map(String))
and calling it with an array [0]
, which represents an empty set of parameters.Pros and Cons
map()
on the rest parameter, which can be slower than using a traditional array.Library
The benchmark uses the String
function to concatenate strings. This is likely due to the fact that JavaScript's built-in string concatenation operator (+
) has some issues with Unicode characters, whereas String()
ensures a consistent behavior across different platforms.
Special JS Feature/Syntax
This benchmark does not use any special or experimental JavaScript features. It only uses standard language syntax and built-in functions.
Other Alternatives
If you're looking for alternative approaches to handling rest parameters, you could consider:
apply()
or call()
to pass an array of arguments to a function.restify
that provides a more robust and expressive way to handle rest parameters.Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preference.