<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
var data = _.range(100000).map(function(i) {
return {
counter: i
}
});
function isOdd(num) {
return num % 2 === 1;
}
function square(num) {
return num * num;
}
function lessThanThreeDigits(num) {
return num.toString().length < 3;
}
var result = R.filter(lessThanThreeDigits,
R.map(square,
R.filter(isOdd,
R.pluck('counter', data))));
var transducer = R.compose (
R.map(R.prop('counter')),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits),
);
var result = R.transduce (
transducer,
(acc, val) => {
acc.push(val);
return acc;
},
[],
data,
);
var result = R.pipe(
R.pluck('counter'),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits)
)(data);
var result = _.chain(data)
.pick('counter')
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
.value();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Ramda without relying on currying or composition | |
Ramda with transducer | |
Ramda with currying and composition | |
Lodash |
Test name | Executions per second |
---|---|
Ramda without relying on currying or composition | 76.0 Ops/sec |
Ramda with transducer | 79.5 Ops/sec |
Ramda with currying and composition | 74.0 Ops/sec |
Lodash | 554448.7 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The benchmark compares the performance of four different approaches to perform a similar task:
filter
, map
, and compose
functions directly)transduce
function to create a pipeline of operations)pipe
function to chain operations)Options compared
The benchmark compares the performance of these four approaches:
filter
, map
, and compose
functions directly from Ramda.transduce
function to create a pipeline of operations that can be composed together.pipe
function to chain operations together.Pros and cons
Here's a brief summary of the pros and cons of each approach:
transduce
function.Libraries used
The benchmark uses two popular JavaScript libraries:
Both libraries provide a set of functions for common tasks like filtering, mapping, and reducing arrays.
Special JS features or syntax
There are no special JS features or syntax used in this benchmark. The code is written using standard JavaScript syntax, with the exception of Ramda's currying and composition features.
Alternatives
If you're looking for alternatives to these libraries, here are a few options:
Keep in mind that the choice of library or alternative depends on your specific use case, personal preference, and performance requirements.