function concatMap1(xs, fn) {
return xs.map(v => fn(v)).reduce((acc, arr) => Array.isArray(arr) ? acc.concat(arr) : acc.concat([arr]), []);
}
function concatMap2(xs, fn) {
var res = [];
for (var i = 0; i < xs.length; i++) {
var x = fn(xs[i], i);
if (Array.isArray(x)) res.push.apply(res, x);
else res.push(x);
}
return res;
}
concatMap1(Array(1000).fill(0), v => [v-1, v, v+1])
concatMap2(Array(1000).fill(0), v => [v-1, v, v+1])
concatMap1([1, 2, 3, 4], v => Array(1000).fill(v))
concatMap2([1, 2, 3, 4], v => Array(1000).fill(v))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
concat 1 | |
concat 2 | |
concat 1 small to large | |
concat 2 small to large |
Test name | Executions per second |
---|---|
concat 1 | 515.3 Ops/sec |
concat 2 | 6537.8 Ops/sec |
concat 1 small to large | 50563.7 Ops/sec |
concat 2 small to large | 41234.9 Ops/sec |
Let's dive into the world of JavaScript benchmarks!
Benchmark Definition
The benchmark compares two different implementations of the concatMap
function, which is used to concatenate arrays in a map-like fashion. The two implementations are:
concatMap1(xs, fn)
: This implementation uses the map
method to create an array of transformed values, and then uses the reduce
method to concatenate these arrays.concatMap2(xs, fn)
: This implementation uses a simple loop to iterate over the input array, transforming each value using the provided function. If the transformed value is an array, it is concatenated with the existing result.Options Compared
The two implementations are compared in terms of their performance. The benchmark measures the number of executions per second for each implementation.
Pros and Cons of Each Approach
concatMap1(xs, fn)
: This approach uses functional programming techniques to create an array of transformed values, which can be more concise and expressive. However, it may incur overhead due to the use of map
and reduce
.concatMap2(xs, fn)
: This approach uses a simple loop to iterate over the input array, which can be more straightforward and efficient. However, it may require more lines of code and be less readable than the first implementation.Library
There is no specific library mentioned in the benchmark definition. The implementations use built-in JavaScript methods such as map
, reduce
, and array concatenation.
Special JS Feature or Syntax
There are no special features or syntax used in this benchmark that are not part of standard JavaScript. Both implementations should be compatible with most modern browsers and JavaScript environments.
Alternatives
If you're interested in exploring alternative approaches, here are a few options:
flatMap
instead of concat
: Some modern browsers and JavaScript engines support the flatMap
method, which can simplify the implementation and improve performance.push
instead of array concatenation: Instead of using concat
or reduce
to concatenate arrays, you could use a simple loop with push
to build up the result array incrementally.concatMap
function.Keep in mind that these alternatives may not be suitable for all use cases and may introduce additional complexity or overhead.