<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 = _.chain(data)
.pick('counter')
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
.value();
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 = R.filter(lessThanThreeDigits,
R.map(square,
R.filter(isOdd,
R.pluck('counter', data))));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Ramda with transducer | |
Ramda with currying and composition | |
Ramda without relying on currying or composition |
Test name | Executions per second |
---|---|
Lodash | 517255.8 Ops/sec |
Ramda with transducer | 83.4 Ops/sec |
Ramda with currying and composition | 72.4 Ops/sec |
Ramda without relying on currying or composition | 74.9 Ops/sec |
Let's break down the provided JSON files and explain what's being tested.
Overview
The benchmark compares the performance of three different ways to achieve a common task: filtering, mapping, and transforming an array of objects in JavaScript. The task involves:
Test Cases
There are four test cases:
The first test case uses Lodash, a popular JavaScript library for functional programming. It defines three functions:
isOdd
: checks if a number is odd.square
: squares a number.lessThanThreeDigits
: returns true if a number has less than two digits.It then creates an array of 100,000 objects with increasing counter values and uses the Lodash chain method (_.chain
) to perform the following operations:
_.pick
._.filter(isOdd)
._.map(square)
._.filter(lessThanThreeDigits)
._.value
.The second test case uses Ramda, another popular JavaScript library for functional programming. It defines a transducer function that takes an accumulator and a value as input and returns the updated accumulator.
The test case creates an empty array ([]
) as the initial accumulator, data as the input array, and then applies the transducer function to process the data:
R.compose
is used to create a composed function from several individual functions:R.map(R.prop('counter'))
: maps each object in the array to its counter value.R.filter(isOdd)
: filters out odd numbers.R.map(square)
: squares each number.R.filter(lessThanThreeDigits)
: filters out numbers with less than two digits.R.transduce
, which updates the accumulator and returns the resulting array.The third test case uses Ramda but without relying on currying or composition. It defines a single function that takes the input array as an argument:
R.filter(lessThanThreeDigits, R.map(square, R.filter(isOdd, R.pluck('counter', data))))
: applies the filters in sequence:R.pluck('counter')
.The fourth test case uses Ramda with currying and composition. It defines the same composed function as in Test Case 2:
R.pipe(R.pluck('counter'), R.filter(isOdd), R.map(square), R.filter(lessThanThreeDigits))
: applies the filters in sequence:Options Compared
The benchmark compares four different approaches:
Each approach has its own trade-offs:
Performance Results
The benchmark results show that the order of performance is:
These results suggest that using a transducer function in Ramda can lead to better performance, especially for large datasets.