<script src="https://rawgit.com/ramda/ramda/master/dist/ramda.js"></script>
var ids = Array.from({ length: 10000 }, (_, index) => index + 1)
var isPositive = i => i % 2 === 0
ids.map(R.identity).filter(isPositive).map(R.inc)
R.pipe(
R.map(R.identity),
R.filter(isPositive),
R.map(R.inc)
)(ids)
const transform = R.pipe(
R.map(R.identity),
R.filter(isPositive),
R.map(R.inc)
)
R.transduce(
transform,
(acc, item) => { acc.push(item); return acc; },
[],
ids,
);
ids.reduce((acc, item) => {
const identity = R.identity(item)
if (!isPositive(identity)) return acc
acc.push(R.inc(identity))
return acc
}, [])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array(map + filter) | |
ramda(map + filter) | |
ramda-transducer(map + filter) | |
Array(combined map reduce) |
Test name | Executions per second |
---|---|
Array(map + filter) | 1456.9 Ops/sec |
ramda(map + filter) | 3168.5 Ops/sec |
ramda-transducer(map + filter) | 1741.2 Ops/sec |
Array(combined map reduce) | 201.4 Ops/sec |
Benchmark Explanation
The provided benchmark is designed to measure the performance of different approaches for filtering and mapping arrays in JavaScript. The test cases compare the execution time of various methods, including:
map()
and filter()
methods.map()
+ filter()**: This approach uses Ramda's functional programming library to chain together three functions:
R.map(R.identity),
R.filter(isPositive), and
R.map(R.inc)`.transduce()
with a custom transformation function: This approach uses Ramda's transduce()
method, which applies a transformation function to an array while accumulating the results in an accumulator.reduce()
method in combination with filter()
.Options Compared
The benchmark compares the performance of these four approaches:
Pros and Cons of Each Approach
Library: Ramda
Ramda is a functional programming library for JavaScript that provides a set of higher-order functions for working with arrays, objects, and functions. The map()
, filter()
, and transduce()
methods are core components of the library, allowing developers to write concise and expressive code.
Special JS Feature/Syntax: None
There is no special JavaScript feature or syntax used in this benchmark that would require explanation beyond Ramda's functional programming concepts.
Other Alternatives
Other alternatives for filtering and mapping arrays might include:
However, these alternatives are not explicitly compared in this benchmark.